Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net mvc routing id parameter

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. And I have a 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 do I need to create separate routes for such situations?

like image 477
Parminder Avatar asked Apr 30 '10 08:04

Parminder


People also ask

What is RouteConfig Cs in ASP.NET MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig. cs file is used to set routing for the application.

What is ASP route ID?

asp-route. The asp-route attribute is used for creating a URL linking directly to a named route. Using routing attributes, a route can be named as shown in the SpeakerController and used in its Evaluations action: C# Copy.


2 Answers

So, you have a parameter named errorno, and you want it to have a value from parameter id. This is obviously the binding problem.

How to solve it:

  1. create a class for model binder:

    public class ParameterBinder : IModelBinder
    {
        public string ActualParameter { get; private set; }
    
        public ParameterBinder(string actualParameter)
        {
            this.ActualParameter = actualParameter;
        }
    
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            object id = controllerContext.RouteData.Values[this.ActualParameter];
            return id;
        }
    }
    
  2. create a custom attribute for custom model binding:

    [AttributeUsage(AttributeTargets.Parameter)]
    public class BindParameterAttribute : CustomModelBinderAttribute
    {
        public string ActualParameter { get; private set; }
    
        public BindParameterAttribute(string actualParameter)
        {
            this.ActualParameter = actualParameter;
        }
    
        public override IModelBinder GetBinder()
        {
            return new ParameterBinder(this.ActualParameter);
        }
    }
    
  3. apply the new attribute to your action parameters as needed:

    public ActionResult ErrorPage(
    [BindParameter("id")]
    int errorno)
    {
        return View();
    }
    

Now your errorno will have the value, which was passed as id for your url.

Note: you can remove the paramter id from the example above, if you are sure you need it solved only for id.

Leaving this way will allow you bind other parameters too.

like image 109
Tengiz Avatar answered Oct 09 '22 00:10

Tengiz


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();
}
like image 36
Fenton Avatar answered Oct 08 '22 23:10

Fenton