ASP.NET MVC: How to force all links to goto 1 page?
Asked Answered
S

1

7

I am trying to figure out how i can create a route configuration so that all URLS will goto the same VIEW (Page).

CUrrently of course if i do for example

/Products/Id

Then this would look in the Products controller.

I would like to always goto my MainController and the same action no matter what the URL is

Is this possible?

Thanks in advance

Sudarium answered 25/9, 2012 at 8:2 Comment(3)
this sounds like a terrible idea.Findley
Can I ask why, is this for a "This site is under development" page or something like that?Amitosis
Yes the reason for this, is AngularJS. It states :- Server side Using this mode requires URL rewriting on server side, basically you have to rewrite all your links to entry point of your application (e.g. index.html)Sudarium
D
20

This is possible to be done with a catchAll route:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "CatchAll",
        "{*url}",
        new { controller = "Main", action = "Index" }
    );
}

Alternatively you could have your default route and put the catchAll route after it so that if no other route is matched, the catchAll route will get it

Dabchick answered 25/9, 2012 at 8:5 Comment(3)
THis sounds great, yes i know it defeats the object but i am using angularjs on client so i really won't be using views on the server. Well 1 view .. yes :-)Sudarium
Thanks Darin. Your answer helped, and as Martin said, I'm also using AngularJS on the client (on a Single Page architecture), and I want everything to go through the same MVC Controller.Zsazsa
Good answer. One comment though, the boilerplate "Default" route is itself a catch all, so you wouldn't be able to have both.Aloysia

© 2022 - 2024 — McMap. All rights reserved.