更改Url.Action(操作,控制器)默认功能

时间:2017-12-21 16:55:27

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

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

 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

请告诉我遗漏的内容:)

P.S 我不想打扰默认的路由设置,即

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

1 个答案:

答案 0 :(得分:0)

在之前映射一条没有action = "Index" 的路线<&strong>&#34;默认&#34;路线:

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"和&#34;默认&#34;路线不受干扰:

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