Asp.net mvc 在MVC中是否可以只路由特定的静态文件?
还有类似的问题。Asp.net mvc 在MVC中是否可以只路由特定的静态文件?,asp.net-mvc,asp.net-mvc-routing,static-files,Asp.net Mvc,Asp.net Mvc Routing,Static Files,还有类似的问题。 但我不想路由所有现有文件(RouteExistingFiles=true),然后忽略所有不需要的类型 MVC或IIS设置可以更精确地告诉它我的意图吗?最后,我采用了以下方法: 在RouteConfig.cs中: public static void RegisterRoutes(RouteCollection routes) { routes.RouteExistingFiles = true; routes.IgnoreRoute("{resource}.a
但我不想路由所有现有文件(
RouteExistingFiles=true
),然后忽略所有不需要的类型
MVC或IIS设置可以更精确地告诉它我的意图吗?最后,我采用了以下方法: 在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”代码>在“一个ASP”中,现在有了。