简体   繁体   中英

C# MVC : MapRoute is adding /index to urls after I changed the default Route

as you know MVC default route is

context.MapRoute(
            "Public_default",
            "{controller}/{action}/{id}",
            new {
                 controller = "Home",
                 action = "Index",
                 id = UrlParameter.Optional
            }
        )

and calling Url.Action("") was returning "/MyController"

After I changed the default route to :

context.MapRoute(
            "Public_default",
            "{controller}/{action}/{id}/{slug}",
            new {
                controller = "Home",
                action = "Index",
                id = UrlParameter.Optional,
                slug = UrlParameter.Optional
            }
        )

Now calling Url.Action("") is returning "/MyController/Index"

The problem is the "/index" . it seems it's ignoring action = "Index" in route defaults.

I think it's happening because of adding {slug} to route.

note: when I call /MyController it's working like before. but Url.Action("") behavior is changed.

How can I solve this problem ?

出乎意料的是,我将两个 Routes都放在了route config中,它解决了问题。但是我认为这对Url.Action()来说是不当行为

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