I have done a route configuration with MVC. The route is so defined:
routes.MapRoute(
name: "Box",
url: "boxes/{id}",
defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional }
);
The problem is that when i call a javascript function from the view Boxes, all the function i call are redirected to the Index function.
For example, if i call var url = "/Boxes/ReturnPrice";
the site don't call this function but the index function.
The index function in boxesController is so defined:
public ActionResult Index()
{
//Code here
return view();
}
When you call /Boxes/ReturnPrice
, It matches your "Box" route definition. the framework will map "ReturnPrice
" from the url to the id
parameter !
You need to define a route constraint which tells that your id property is of type int ( I assume it is int in your case) . Also you need to make sure that you have a generic route definition exists to handle your normal requests with the format controllername/actionmethodname
.
You can define the route constraint when defining the route using regex.
routes.MapRoute(
name: "Box",
url: "boxes/{id}",
defaults: new { controller = "Boxes", action = "Index", id = UrlParameter.Optional },
constraints: new { id = @"\d+" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
With this Boxes/ReturnPrice
will go to ReturnPrice action method while Boxes/5
will go to Index action method with value 5 set to Id param.