简体   繁体   English

更改Url.Action(Action,Controller)默认功能

[英]change Url.Action(Action,Controller) default functionality

I don't know what is wrong with my routing configuration. 我不知道我的路由配置出了什么问题。

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

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

http://localhost:61759/agent http:// localhost:61759 / agent

rather than 而不是

http://localhost:61759/Agent/Index http:// localhost:61759 / Agent / Index

Please let me know what is missing :) 请让我知道缺少了什么:)

PS I don't want to disturb the default routing settings ie PS我不想打扰默认路由设置,即

  1. http://localhost:61759 should route to default page http:// localhost:61759应该路由到默认页面
  2. http://localhost:61759/Agent should route to http://localhost:61759/Agent/Index http:// localhost:61759 / Agent应该路由到http:// localhost:61759 / Agent / Index

Map a route without action = "Index" before the "Default" route: 在“默认”路由之前映射一个没有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 }
);

Now @Url.Action("Index", "Agent") returns "/Agent/Index" and "Default" route is undisturbed: 现在, @Url.Action("Index", "Agent")返回"/Agent/Index" ,“ Default”路由不受干扰:

  1. http://localhost:61759 should route to default page http:// localhost:61759应该路由到默认页面
  2. http://localhost:61759/Agent should route to http://localhost:61759/Agent/Index http:// localhost:61759 / Agent应该路由到http:// localhost:61759 / Agent / Index

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM