2

I have specified the following routes:

routes.MapRoute(
            "myRoute1", 
            "{controller}/{action}/{id}", 
            new { controller = "Home", action = "Index", id = UrlParameter.Optional });

routes.MapRoute(
             "myRoute2", 
             "public/{controller}/{action}/{id}", 
              new { controller = "PublicHome", action = "Index", id = UrlParameter.Optional });

and would expect the following url to work

[http://localhost:58658/public]

Since in myRoute2 for [/public] url, I specified the default values for controller and action but this doesn't seem to work. Any ideas ?

Thanks

2
  • pure guess: put / infront of public/{controller}/{action}/{id} Commented Jul 25, 2012 at 9:37
  • The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character. Commented Jul 25, 2012 at 9:48

3 Answers 3

3
routes.MapRoute(
            "myRoute1", 
            "public/{controller}/{action}/{id}", 
            new { controller = "PublicHome", action = "Index", id = UrlParameter.Optional });

Install RouteDebugger. This will tell you if your route will be hit or not for a given url.

If you are matching multiple routes, the first one will be processed. RouteDebugger will show you all matches. It's up to you to reorder them appropriately.

If ROuteDebugger doesn't work on 404 pages, turn on custom errors. Example:

<customErrors mode="On" defaultRedirect="err/index">
  <error statusCode="404" redirect="err/notfound" />
</customErrors>`
Sign up to request clarification or add additional context in comments.

10 Comments

I had public before instead of {public} and still it doesn't work.
I get the following exception: The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /public/
Install RouteDebugger to ensure your route is matched. Do you have a controller called "PublicHome" with a GET action named "Index" that has zero mandatory parameters?
Thats correct I have a controller called "PublicHome" with a Get action index with zero mandatory parameters.
I have installed RouteDebugger not sure how is it going to help me because hitting /public returns 404.
|
2

Change the order of the route declarations, your first (default) route matches the url and searches for PublicController.

Comments

0

You should not have put curly braces around public in you route, because it's nota parameter.

1 Comment

I had public before instead of {public} and still it doesn't work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.