Michael
Michael

Reputation: 97

ASP.Net C# MVC create exception to a global route

I am writing a URL Shortener application.

When someone enters root.com/whatever, they are redirected to a configured URL.

I managed to create a global route which will catch the paths after the root ("whatever" above) and execute the corresponding redirection successfully.

My problem and question is this: The admin interface is at root.com/admin and when I try to access that, I get the global controller. How do I make an exception to the global controller for "admin"?

Here is what I have in my route config right now:

routes.MapRoute(
    name: "Admin",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Global",
    url: "{suffix}",
    defaults: new { controller = "Home", action = "Index", suffix = UrlParameter.Optional }
);

For the first route, I also tried:

routes.MapRoute(
    name: "Admin",
    url: "admin/{action}/{id}",
    defaults: new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);

And I also tried putting it second in the file.

Upvotes: 0

Views: 251

Answers (1)

Frank Thomas
Frank Thomas

Reputation: 380

I don't know if this will help you, but take a look at this stack overflow posting: ASP.Net MVC: Routing issue which throwing exception This person reduced the more specific route and it worked for him.

Upvotes: 1

Related Questions