答案 0 :(得分:0)
最后,我按照以下方式制作:
在RouteConfig.cs中:
public static void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*route}", new { route = new AnyExistingFileExceptRazorView() });
...
}
AnyExistingFileExceptRazorView
是自定义路由约束的地方:
public class AnyExistingFileExceptRazorView : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values,
RouteDirection routeDirection)
{
string filePath = httpContext.Server.MapPath(httpContext.Request.Url.AbsolutePath)
.ToLower();
return File.Exists(filePath)
&& !( filePath.EndsWith(".cshtml") || filePath.EndsWith(".vbhtml"));
}
}
ToLower()
被调用是因为我不确定哪个URL有,并且我检查期望小写的结尾。
也可以比较Path.GetExtension(fielPath) == ".cshtml";