简体   繁体   中英

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

http://localhost:61759/agent

rather than

http://localhost:61759/Agent/Index

Please let me know what is missing :)

PS I don't want to disturb the default routing settings ie

  1. http://localhost:61759 should route to default page
  2. http://localhost:61759/Agent should route to http://localhost:61759/Agent/Index

Map a route without action = "Index" before the "Default" route:

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:

  1. http://localhost:61759 should route to default page
  2. http://localhost:61759/Agent should route to http://localhost:61759/Agent/Index

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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