这是之前我做了什么:
public class MvcApplication : HttpApplication
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
MapRoute(routes, "", "Home", "Index");
/* other routes */
MapRoute(routes, "{*url}", "Documentation", "Render");
}
}
既然不匹配的路由被传递给DocumentationController
。我的文档控制器如下所示:
public class DocumentationController : Controller
{
public ActionResult Render(string url)
{
var md = new MarkdownSharp.Markdown();
// The path is relative to the root of the application, but it can be anything
// stored on a different drive.
string path = Path.Combine(Request.MapPath("~/"), GetAppRelativePath().Replace('/', '\\')) + ".md";
if (System.IO.File.Exists(path))
{
string html = md.Transform(System.IO.File.ReadAllText(path));
return View("Render", (object)html);
}
// return the not found view if the file doesn't exist
return View("NotFound");
}
private string GetAppRelativePath()
{
return HttpContext.Request.AppRelativeCurrentExecutionFilePath.Replace("~/", "");
}
}
这一切确实是找到降价文件,并相应地呈现它们。要为你的情况更新这个,你可能要做到以下几点:
routes.MapRoute(
"Parameter1",
"{controller}/{action}/{lang}/{*url}",
new { controller = "Manuals", action = "Download", lang = "en-US", prod = "sample" }
);
确保它是{controller}/{action}/{lang}/{prod}
路线之后。这应当引起这样的URL /Manuals/Product/en-US/images/image.svg
甚至images/image.svg
(如果浏览器是/Manuals/Product/en-US/sample
调用的Download
动作。然后,您可以适应我写信给其映射URI的物理位置的代码。你可能会碰到的是一个问题“图像”被认为是产品和/Manuals/Product/en-US/images
会认为它的产品。
的形象动作可以可以看看如下。
public ActionResult Download(string url)
{
/* figure out physical path */
var filename = /* get filename form url */
var fileStream = [...];
Response.Headers.Remove("Content-Disposition");
Response.Headers.Add("Content-Disposition", "inline; filename=" + filename);
string contentType = "image/jpg";
return File(fileStream, contentType, filename);
}
你可以在MSDN的FileResult的更多信息。
好,非常感谢,但我不知道我理解你正确...我不希望图像被下载,他们应该出现在页面上。但是,当我使用相对路径他们没有工作如上所述... – Anders 2012-03-10 23:36:30
也许下载是错误的“空话”。请参阅下载的更新版本。通过将ContentComposition标题设置为内联,浏览器将显示图像,而不是下载它。你将不得不做一些工作来找出url中的实际路径名等。我希望澄清它。 – bloudraak 2012-03-25 21:33:46