【问题标题】:change Url.Action(Action,Controller) default functionality更改 Url.Action(Action,Controller) 默认功能
【发布时间】:2018-06-04 09:32:10
【问题描述】:

我不知道我的路由配置出了什么问题。

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

但是@Url.Action("Index","Agent") 返回

http://localhost:61759/agent

而不是

http://localhost:61759/Agent/Index

请让我知道缺少什么:)

附言 我不想打扰默认路由设置,即

  1. http://localhost:61759 应该路由到默认页面
  2. http://localhost:61759/Agent 应该路由到 http://localhost:61759/Agent/Index

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing aspnetboilerplate


    【解决方案1】:

    映射没有action = "Index"“默认”路线之前的路线:

    routes.MapRoute(
        name: "NoDefaultAction",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", id = UrlParameter.Optional }
    );
    
    // Code below unchanged
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
    

    现在@Url.Action("Index", "Agent") 返回"/Agent/Index" 并且“默认”路由不受干扰:

    1. http://localhost:61759 应该路由到默认页面
    2. http://localhost:61759/Agent 应该路由到 http://localhost:61759/Agent/Index

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 2014-08-03
      • 1970-01-01
      相关资源
      最近更新 更多