0

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?

1
  • 1
    You may have to map it before that default one. Commented Jun 10, 2014 at 17:56

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So rule of thumb, always have the most generic route last.
@Gudradain You're probably looking for something where the controller is optional? Use something like Report/MyReport/{action}/{id} as your URL, and then use the default controller MyReport?
@StevenV Maybe... But the thing is that Report/... is a section. There are many reports in it (MyReport1, MyReport2, MyReport3, etc...). I guess I can add them one by one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.