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

问题描述 投票:0回答:1

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

 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
c# asp.net-mvc asp.net-mvc-5 asp.net-mvc-routing aspnetboilerplate
1个回答
0
投票

在“默认”路线之前映射没有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
© www.soinside.com 2019 - 2024. All rights reserved.