如何从基本URL设置MapRoute?

时间:2014-06-20 16:39:50

标签: c# asp.net global-asax

我已成功设法将网址www.mysite.com/home/directory路由到主页:

 Routes.MapRoute(
    "forbidden1",
    "{controller}/directory",
    new {                               
       controller = "Home",
       action = "Index",
    });

...但是我如何使用URL www.mysite.com/directory执行相同操作,因为mysite.com 主页???

1 个答案:

答案 0 :(得分:0)

您是否试图阻止文件夹访问?如果是这样,这不是正确的方法。

Code on loan from this answer 这将阻止请求,但允许您的代码访问文件夹。

<configuration>
   <system.webServer>
       <security>
          <requestFiltering>
               <hiddenSegments>
                   <add segment="My_Directory" />
               </hiddenSegments>
           </requestFiltering>
       </security>
   </system.webServer>
</configuration>

编辑:

如果你想捕获所有糟糕的MVC路由,请使用类似的东西......

routes.MapRoute(
    "TrapBadRequests",
    "{*url}",
    new { controller = "Home", action = "Default" }
);