5

I want to route REST service url in the following way:

/User/Rest/ -> UserRestController.Index()
/User/Rest/Get -> UserRestController.Get()

/User/ -> UserController.Index()
/User/Get -> UserController.Get()

So basically I'm making a hardcoded exception for Rest in the url.

I'm not very familiar with MVC routing. So what would be a good way to achieve this?

Eli
  • 17,397
  • 4
  • 36
  • 49
Saab
  • 981
  • 3
  • 11
  • 34

1 Answers1

16

Wherever you register your routes, commonly in global.ascx

        routes.MapRoute(
            "post-object",
            "{controller}",
            new {controller = "Home", action = "post"},
            new {httpMethod = new HttpMethodConstraint("POST")}
        );

        routes.MapRoute(
            "get-object",
            "{controller}/{id}",
            new { controller = "Home", action = "get"},
            new { httpMethod = new HttpMethodConstraint("GET")}
            );

        routes.MapRoute(
            "put-object",
            "{controller}/{id}",
            new { controller = "Home", action = "put" },
            new { httpMethod = new HttpMethodConstraint("PUT")}
            );

        routes.MapRoute(
            "delete-object",
            "{controller}/{id}",
            new { controller = "Home", action = "delete" },
            new { httpMethod = new HttpMethodConstraint("DELETE") }
            );


        routes.MapRoute(
            "Default",                          // Route name
            "{controller}",       // URL with parameters
            new { controller = "Home", action = "Index" }  // Parameter defaults
            ,new[] {"ToolWatch.Presentation.API.Controllers"}
        );

In your controller

public ActionResult Get() { }
public ActionResult Post() { }
public ActionResult Put() { }
public ActionResult Delete() { }
Jason Watts
  • 3,800
  • 29
  • 33