1

I am still very new to routing with asp.net mvc, so perhaps this is obvious and I am just missing the answer...

I have a controller named 'pages', and it has several action results, 'Information', 'History' etc. Each action result takes a string, and from that, it returns a View based on the name of the string. So...

Pages/Information/About Pages/Information/Products Pages/History/Employees

etc. The Controller is named 'Pages', of course. I'm wondering if I can use Routing to remove the 'Pages' part of the URL on the user side, just for a more user-friendly approach?

1 Answer 1

3

Yep you can do that:

context.MapRoute(
            "Pages_History_Employees",
            "History/Employees", // URL with parameters
            new { controller = "Pages", action = "History" }
        );

Just specify the controller as Pages and specify whatever url you want as the second parameter. What this is saying is I want to route the History/Employees URI to the Pages controller and use the History action to handle this route.

Just be careful that if you have the default MVC route in there that it appears at the end of your routes or it will match this route first. Then you will get an error since it will be looking for the History controller with the Employees action.

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

Comments

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.