【问题标题】:MapRoute index.asp not routing to controllerMapRoute index.asp 没有路由到控制器
【发布时间】:2019-05-17 08:37:53
【问题描述】:

我有以下 MapRoute 可以工作并路由到控制器“酒店”

 routes.MapRoute("Hotel", "en/hotels/london/victoria/my_hotel_name/", 
            new { controller = "Hotel", action = "Index" }, namespaces: new[] { "UrlRouter.Router.Controllers" } );

但是,如果用户在路径中输入文件名 index.asp,它不会路由到控制器,而只是加载 .ASP 文件中的实际内容,这是我不想要的。我希望它路由到控制器,所以我可以控制返回给用户的内容。

我尝试的路线是

routes.MapRoute("Hotel", "en/hotels/london/victoria/my_hotel_name/index.asp", 
            new { controller = "Hotel", action = "Index" }, namespaces: new[] { "UrlRouter.Router.Controllers" } );

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc maproute


    【解决方案1】:

    经过进一步调查,我发现了以下作品。在 web.config 中添加

     <modules>
      <remove name="UrlRoutingModule-4.0" />
      <add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
    

    然后在注册路由

        public static void RegisterRoutes(RouteCollection routes)
        {
    
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            routes.RouteExistingFiles = true;
    
            routes.MapRoute("Hotel", "en/hotels/london/victoria/my_hotel_name/{*.*}",
                new { controller = "Hotel", action = "Index", }, namespaces: new[] { "UrlRouter.Router.Controllers" });
         }
    

    这允许我将这两个 URL 路由到我的酒店控制器并返回 MVC 视图

    www.mydomain.com/en/hotels/london/victoria/my_hotel_name/

    www.mydomain.com/en/hotels/london/victoria/my_hotel_name/index.asp

    如果有人有更好的建议,请发表您的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      相关资源
      最近更新 更多