這是之前我做了什麼:
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