4

I have very simple question. My site, based on ASP.NET MVC, can have many urls, but all of them should bring to the one controller. How to do that?

I suppose I need some magic in Global.asax but I don't know how to create route that will redirect any url to the specific controller.

For example I have url /about, /product/id etc. but all of them should be really bring to the content/show where the parts of url will be recognized and the decision what information to show will be make. It's some like CMS when you cannot define routes in advance. Is this information enough?

Thanks

4
  • 3
    Also, it would be helpful if you gave some examples of URLs and what you want to do with them in the controller. Commented May 3, 2011 at 1:01
  • The problem is not that I need some "infinite" parameters but the url may be short like /products/id but it should go to the Content.show controller always instead Products.Index for example. Commented May 3, 2011 at 16:53
  • 1
    A single controller might sound like a way to make things simpler, but in the long run your code will be more organized and easier to maintain if you maintain your functionality into separate controllers, just like the MVC folks suggest. Commented May 3, 2011 at 20:19
  • as someone who's now looking at a solution where someone's done exactly this... if anyone else is looking to do this.. please just no! Commented Sep 25, 2015 at 14:22

2 Answers 2

21

This sounds like a horrible idea, but, well, if you must;

routes.MapRoute(
    "ReallyBadIdea",
    "{*url}",
    new { controller = "MyFatController", action = "MySingleAction" }
    );

This routes everything to a single action in a single controller. There's also {*path} and other URL patterns should you want slightly more flexibility.

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

2 Comments

Why bad idea? There are legit usage for it. If your app is doing URL rewrite in browser and loading content accordingly then regardless of url you want to load the base page. Ajax will take care of rest later on in client side.
Yes mamu, it's different than a bad idea. I think programming world is a creation world; Not a usage world. Thanks mamu to make public your creation idea.
2

Ideally you should try and specific with your routes, for example if you have a URL that is /products/42 and you want it to go to a generic controller you should specify it explicitly like

routes.MapRoute(
    "Poducts",
    "products/{id}",
    new { controller = "Content", action = "Show", id = UrlParameter.Optional }
    );

then you would specify another route for something else like /customers/42

routes.MapRoute(
        "Customers",
        "customers/{id}",
        new { controller = "Content", action = "Show", id = UrlParameter.Optional }
        );

this may seem a little verbose, and creating a single route might seem cleaner, but the issue a single route is you will never get a 404 and will have to handle such things in code.

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.