views:

140

answers:

3

Hi Experts,

I am working on a website in asp.net mvc. I have a route

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

which is the default route. Now I have method

public ActionResult ErrorPage(int errorno)
{
    return View();
}

Now if I want to run this code with http://something/mycontroller/Errorpage/1 it doesn't work. But if I change the parameter name to id from errorno it works.

Is it compulsory to have same parameter name for this method. Or I need to create separate routes for such situations.

Regards

Parminder

A: 

You could either rename the parameter in the default root (which probably is not a good idea) or rename it in the action method. Adding another root will not help because it will be the same as the default one and given an url the routing engine cannot distinguish between the two and will always take the first one in the list.

Darin Dimitrov
A: 

try to use the same name of parameter in action method as in in the route table url parameter.

global.asx

routes.MapRoute(

                        "Default", // Route name
                        "{controller}/{action}/{id}", // URL with parameters
                        new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults

                );

myController

public ActionResult ErrorPage(int id)

    {
        return View();
    }
QuBaR
i know it works this way. but i dont want it.
Parminder
A: 

Option 1

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

public ActionResult ErrorPage(int id)
{
    return View();
}

Option 2

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

public ActionResult ErrorPage(int errorno)
{
    return View();
}

Option 3

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

public ActionResult ErrorPage(int id)
{
    int errorno = id;
    return View();
}
Sohnee