这是我之前做过的:
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} 路由之后。这应该会导致诸如/Manuals/Product/en-US/images/image.svg 甚至images/image.svg 之类的URL(如果浏览器在/Manuals/Product/en-US/sample 中调用Download 操作。然后您可以调整我编写的代码以将该URI 映射到物理位置。您可能会遇到的一个问题是“图像”被认为是产品,而/Manuals/Product/en-US/images 会认为它是产品。
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的信息。