1

I'm developing an ASP.NET MVC4 application and I'm new. I have two MapRoutes for routing.

routes.MapRoute("Char", 
                "ABC/Alpha/{number1}", 
                 defaults: new { Controller = "ABC", action = "Alpha" });           

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

I didn't assign any default value to the number1 in the MapRoutes. But I know it is mandatory because it is in the URL path.

If I didn't provide any parameter in the url (when I run the app), I should get an error but I didn't.

Eg.:

http://localhost:32066/ABC/alpha/value---- getting desired output.
http://localhost:32066/abc/alpha/     ---- expected an error but there is no error.

Can anyone help me?

3 Answers 3

2

It doesn't match the first route, but it matches the second route. So it gets the controller/action = ABC/Alpha, and Id is optional.

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

1 Comment

Make sure you mark an answer if your problem was solved.
1

Kyle is right, the second route (which is the global default) matches the url which is valid hence you don't see an error. I am guessing you are following one of the official Microsoft training videos that simulates an error in the request due to bad url that does not honor the route path. To simulate the error, remove the default route, compile the project and run. Only this time specify the second url you have listed you should see the error. Hope this helps.

Comments

0

The Route path is checked from top to bottom, so in the first case

{Controller}/{Action}/{ID}

is passed by you (mention the id is must here) so the First routepath works

but Magic you was expecting the second case to throw an error, and it should have because if your Action is like

public ActionResult ActionName(int id)
{
//some Code
}

then for sure it would have thrown error like Action

"Cannot convert null to 'int' because it is a non-nullable value type"

and i'm Pretty sure your Action will be having either

 public ActionResult(int? id)
 {//Some Code here}

or

Public ActionResult()
{
//Some Code here
 }

1 Comment

your are right. Can you please tell me what is "int?" you passed as parameter. public ActionResult(int? id) {//Some Code here}

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.