2

I have a RouteConfig like this

public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index"} ); } } 

But when i am trying use "http://example.com/controllername/action/166699406". It is getting the error below. What I am trying to achieve is, I want to make the id parameter mandatory. If they dont have an id parameter in the url, it should not hit a route and should show 404. I know we can achieve this by null checking the parameter in the controller. Is there any way manage that in route config itself.

No route in the route table matches the supplied values.

Exception Details: System.InvalidOperationException: No route in the route table matches the supplied values.

0

3 Answers 3

0

You should handle this in web.confing not in routing.

<customErrors mode="On" defaultRedirect="/error/default"> <error statusCode="403" redirect="/error/restricted"/> <error statusCode="404" redirect="/Default/DefaultRoute"/> <error statusCode="500" redirect="/error/problem"/> </customErrors> 
Sign up to request clarification or add additional context in comments.

Comments

0

If I understand correctly you want to add a constraint to the route

constraints: new { id = @"([0-9]+)" } 

this will mean the route will only match when id is a number, it is mandatory.

in full:

routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index"}, constraints: new { id = @"([0-9]+)" } ); 

Note that the route you gave in the question should match the rule you have currently so I'd double check your controller, action and spelling in case there is simply a typo there somewhere.

2 Comments

I tried this but getting the same error, when i am trying to access example.com/controllername/action/166699406
I'm guessing there is something wrong with that url, try writing a simple test to verify your routes: nuget.org/packages/MvcRouteTester.MVC5
0

You can refer to the following posts:

How can i make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

and

http://richarddingwall.name/2008/08/17/strategies-for-resource-based-404-errors-in-aspnet-mvc/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.