Wil*_*ill 11 asp.net-mvc-routing asp.net-mvc-4
我有一个ASP.NET MVC 4应用程序(使用.NET framework 4.5)和无扩展名的URL.该站点包含一些静态文件,但所有无扩展请求都应该进入MVC路由.
这一切都适用于以下请求:
但是,如果我发出/ fr的请求,我会收到错误:
HTTP Error 403.14 - Forbidden,
The Web server is configured to not list the contents of this directory.
Run Code Online (Sandbox Code Playgroud)
我知道这是因为磁盘上确实存在一个/ fr目录,但我仍然希望将此请求映射到我的MVC应用程序.它不是删除fr目录的选项,因为它包含一些静态文件.
这可能吗?我已经尝试添加runAllManagedModulesForAllRequests="true"到system.webServer中的modules元素(我真的不想这样做,但它无论如何都没有帮助).
编辑 - 如果它有用,这里是路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("cid/{*pathInfo}");
routes.MapRoute(
"Page",
"{*PageId}",
new { controller = "Page", action = "Page" }, // Parameter defaults
new { pageId = @"^(.*)?$" } // Parameter constraints
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
阻止访问本地文件夹和文件的最简单方法是设置RouteCollection.RouteExistingFiles标志,让ASP.NET处理以物理文件为目标的URL.因此,将您的路线注册更改为:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("cid/{*pathInfo}");
routes.RouteExistingFiles = true;
routes.MapRoute(
"Page",
"{*PageId}",
new { controller = "Page", action = "Page" }, // Parameter defaults
new { pageId = @"^(.*)?$" } // Parameter constraints
);
}
Run Code Online (Sandbox Code Playgroud)
您的"Default"路线不是必需的,因为"Page"它实际上是一个包罗万象的案例.
另一种方法是更改IIS配置,使ASP.NET MVC路由优先于IIS目录列表.在IIS7中,进入您的网站,从IIS部分选择模块,然后从操作选项卡中查看有序列表.将UrlRoutingModule移到DirectoryListingModule上方.
作为旁注,根据您的评论,我了解您只有一个控制器,只需一个操作.除了定义的静态资源外,该操作将为所有请求提供服务IgnoreRoute.这不是推荐的设置,因为您失去了MVC架构的所有好处.此外,您会发现您的Page操作会迅速增长,以包含越来越多的案例.这正是路由和控制器旨在解决的问题.
如果你认为单一的catch-all方法对你来说是最好的解决方案,那么你真的不需要MVC,你最好使用Web API,它的请求开销要小得多.
| 归档时间: |
|
| 查看次数: |
2539 次 |
| 最近记录: |