0

I want to use custom URL route:

http://localhost/MyController/MyView/11/25?someID=0

to do so, I have the following action:

[Route("{page}/{size}")]
public ActionResult MyView(int page = 0, int size = 25, FilterModel filterModel = null)
{
   ...
}

but I get the 404 error. What's wrong ? For example, that URL works:

http://localhost/MyController/MyView?someID=0
NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Tony
  • 12,405
  • 36
  • 126
  • 226

2 Answers2

1

The route you defined:

[Route("{page}/{size}")]
public ActionResult MyView(int page = 0, int size = 25, FilterModel filterModel = null)
{
   ...
}

Does not match the URL you want:

http://localhost/MyController/MyView/11/25?someID=0

You need to ensure they are the same (assuming you are not using a RoutePrefix attribute on the controller):

[Route("MyController/MyView/{page}/{size}")]
public ActionResult MyView(int page = 0, int size = 25, FilterModel filterModel = null)
{
   ...
}

The reason why the URL http://localhost/MyController/MyView?someID=0 works is most likely that it is matching your Default route and passing the correct controller and action names to the framework to get to the action method.

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

Note also that you need to have route.MapMvcAttributeRoutes() before your default route in order to enable attribute routing.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
0

This is attribute routing. Apologies on the last answer.

routes.MapRoute( name: “MyView”, url: “{page}/{size}”, defaults: new { controller = “MyController”, action = “MyView” }, constraints: new { page= "", size="" } );

https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/