Asp.net-mvc – ASP.Net MVC routing issue url not working

asp.net-mvcasp.net-mvc-routing

I created two action methods with a different name in the home controller

public ActionResult Default()
{
    ViewData["Message"] = "Welcome to ASP.NET MVC!";
    return View("index");
}

public ActionResult Index(int a)
{
    ViewData["Message"] = "Welcome to ASP.NET MVC! and Your Age is " + a;
    return View();
}

And my routing code look like:

routes.MapRoute(
  "Default1", // Route name
  "{Home}/{ID}", // URL with parameters
  new { controller = "Home", action = "Index", id =UrlParameter.Optional});

routes.MapRoute(
    "Default2", // Route name
    "{Home}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

But still getting problem

when I type URL like http://localhost:7221 then home is coming and Default() method invoking but if I type URL like

http://localhost:7221/Home then getting error. to handle this situation i define route like

routes.MapRoute(
    "Default2", // Route name
    "{Home}", // URL with parameters
    new { controller = "Home", action = "Default" }
);

But it is not working…….can u tell me why.

If I type URL like http://localhost:7221/Home/88 then Index(int a) method should be called but getting error. why

I want that when I type URL http://localhost:7221 or http://localhost:7221/Home then Default() should be called and when I will type

http://localhost:7221/Home/88 then Index(int a) should be invoked. What is wrong in my route? How can I rectify it? if possible rectify my route code. thanks

Best Answer

This rule should actually cover everything you need:

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

Note the following:

  1. The parameter name needs to match the parameter on the method, you had id and a - so the framework can't match them. In the rule above, I have put a in the mapping rule to match your method, but you should give it a better name in both places.

  2. This rule defaults to /Home/Index if no controller or action are supplied. If you want to hit the action you have named Default you would go to /Home/Default

  3. If you supply /Home/Index/21 it will call the Index method - but if you don't supply an age it will have a problem as you have no method to match the rule. You need to add a method public ActionResult Index() or use a default value public ActionResult Index(int a = 0)

Here are some examples of URLs that should work against this rule.

  • http://yourapp/ - will go to /Home/Index
  • http://yourapp/Home - will go to /Home/Index
  • http://yourapp/Home/Default - will go to /Home/Default
  • http://yourapp/Home/Index - see my notes above - you need a method to support this
  • http://yourapp/Home/Index/21 - will go to /Home/Index and pass a as 21
Related Topic