The question is as simple as the title.
Is it possible to have a route that looks like this: {controller}/{id}/{action}
?
This is what I have in code right now (just a simple function) (device
is my controller):
[HttpGet]
[Route("Device/{id}/IsValid")]
public bool IsValid(int id) {
return true;
}
But when I go to the following URL the browser says it can't find the page: localhost/device/2/IsValid
.
And when I try this URL, it works just fine: localhost/device/IsValid/2
So, is it possible to use localhost/device/2/IsValid
instead of the default route localhost/device/IsValid/2
? And how to do this?
Feel free to ask more information! Thanks in advance!
You are using Attribute routing. Make sure you enable attribute routing.
Attribute Routing in ASP.NET MVC 5
For MVC RouteConfig.cs
public class RouteConfig {
public static void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
//...Other code removed for brevity
}
}
In controller
[RoutePrefix("device")]
public class DeviceController : Controller {
//GET device/2/isvalid
[HttpGet]
[Route("{id:int}/IsValid")]
public bool IsValid(int id) {
return true;
}
}
try using this before Default
route in RoutingConfig
config.Routes.MapHttpRoute(
"RouteName",
"{controller}/{id}/{action}"
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With