0

I know it's the general approach:

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

But If I try like this:

 routes.MapRoute
    (
          name: "Defaults",
          url: "Home/Index/1"
    );

It will not give any error to run the project. I have Controller = Home Action = Index() in my HomeController but why It is giving error while I type this http://localhost:1702/Home/Index/1in my browser?

Error: The matched route does not include a 'controller' route value, which is required.

Which part is mandatory in Routing? Another question is: Why we need Route Name?? If I give name="", it works fine even more than one Route is exists like name=""

  • `routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );`? – Grizzly Aug 30 '16 at 12:15
  • No. If you try like me it will not give you any error. I know the routes.MapRoute become like you. By default it has three parts. name, url and defaults, I know this – Lutfor Rahman Aug 30 '16 at 12:17
  • Maybe [this](http://stackoverflow.com/questions/23752298/mvc-routing-why-i-can-not-ignore-defaults-the-matched-route-does-not-include-a) can help? your question seems like a duplicate. – Grizzly Aug 30 '16 at 12:26
  • May be but what about the second question? – Lutfor Rahman Aug 30 '16 at 12:34
  • If you just use: `http://localhost:1702` does this go to `home/index/1`? – Ric Aug 30 '16 at 12:34
  • *Which part is mandatory in Routing? Another question is: Why we need Route Name?* The answer to that can be found [here](https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionextensions.maproute(v=vs.118).aspx) – Grizzly Aug 30 '16 at 12:40

2 Answers2

0

you need this:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
Ric
  • 12,855
  • 3
  • 30
  • 36
  • I know it's the general approach. If I dont use defaults what will be the problem?? – Lutfor Rahman Aug 30 '16 at 12:19
  • @LutforRahman There is an [overloaded method](https://msdn.microsoft.com/en-us/library/system.web.mvc.routecollectionextensions.maproute(v=vs.118).aspx) that states that you don't need `defaults`.. so why don't you try it? – Grizzly Aug 30 '16 at 12:22
  • Why this will give overload if I indicate my Controller, action and id in my browser? Will you please elaborate? – Lutfor Rahman Aug 30 '16 at 12:24
0

url is to define the url structure for end user. You can even change it to

url: "{action}/{controller}/{id}"

So to understand which is controller and which is action you have to provide the url structure.