Is it possible to define a parameter before an action in the url

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 the code right now (just a simple function) ( device- my controller):

[HttpGet]
[Route("Device/{id}/IsValid")]
public bool IsValid(int id) {
    return true;
}

But when I turn to the next the URL, the browser says it can not find the page: localhost/device/2/IsValid.

And when I try to use this url, it works fine: localhost/device/IsValid/2

So, can you use the localhost/device/2/IsValiddefault route instead localhost/device/IsValid/2? And how to do it?

Feel free to ask for more info! Thanks in advance!

+4
source share
2 answers

. , .

ASP.NET MVC 5

MVC RouteConfig.cs

public class RouteConfig {

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        //...Other code removed for brevity
    }
}

[RoutePrefix("device")]
public class DeviceController : Controller {
    //GET device/2/isvalid
    [HttpGet]
    [Route("{id:int}/IsValid")]
    public bool IsValid(int id) {
        return true;
    }
}
+4

Default RoutingConfig

config.Routes.MapHttpRoute(
    "RouteName",
    "{controller}/{id}/{action}"
    );
+2

All Articles