简体   繁体   中英

Custom URL in c# MVC 4

I'm trying to add custom URL to my application. For example, I have a report section and I want all the Controller in that section to start with "Report". Currently, my URL look like :

localhost:12345/MyReport

but I want it to look like

localhost:12345/Report/MyReport

MyReport is the name of my controller.

I tried to change the following code in Global.asax

            routes.MapRoute(
                "Default", 
                "{controller}/{action}/{id}", 
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

By

            routes.MapRoute(
                "Default", 
                "{controller}/{action}/{id}", 
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
            );

            routes.MapRoute(
                "Report",
                "Report/{controller}/{action}/{id}",
                new { controller = "MyReport", action = "Index", id = UrlParameter.Optional }
            );

But I get a page not found error when I try to access localhost:12345/Report/MyReport ... Any clue?

As crazy as this is at first, order matters. Routing will find the first route that matches, and serves it. Try putting the the default one after your new custom one.

Because of that, you're going to the controller of Report , and attempting to find the action of MyReport . Which is indeed not found.

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