Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define the PUT method in routing only limit to the Put methods in controller without parameter?

Here is the routing configuration in WebApiConfig.cs:

config.Routes.MapHttpRoute(
    name: "DefaultApiPut",
    routeTemplate: "api/{controller}",
    defaults: new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
);


config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Post, HttpMethod.Delete) }
);

Here is my controller:

public class MyController : ApiController {
    [HttpPut]
    public void Put()
    {
        //blah
    }
}

Somehow when the client sents the PUT request with the URL /api/myController/12345, it still maps to the Put method in MyController, I am expecting an error like resource not found.

How to force the Put method only accept the request without a parameter?

Thanks in advance!

like image 606
zs2020 Avatar asked Sep 26 '12 20:09

zs2020


People also ask

Which is the attribute used to specify the routing pattern with the web API?

Routing is how Web API matches a URI to an action. Web API 2 supports a new type of routing, called attribute routing. As the name implies, attribute routing uses attributes to define routes. Attribute routing gives you more control over the URIs in your web API.

What is Route controller?

Routing controllers allow you to create the controller classes with methods used to handle the requests. Now, we will understand the routing controllers through an example. Step 1: First, we need to create a controller. We already created the controller named as 'PostController' in the previous topic.

Why we use attribute routing in web API?

Attribute routing gives you more control over the URIs in your web API. For example, you can easily create URIs that describe hierarchies of resources. The earlier style of routing, called convention-based routing, is still fully supported. In fact, you can combine both techniques in the same project".


2 Answers

This works to constrain http method on routes:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
    config.Routes.MapHttpRoute(
       name: "LocationApiPOST",
       routeTemplate: "api/{orgname}/{fleetname}/vehicle/location",
       defaults: new { controller = "location" }
       constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) }
   );

    config.Routes.MapHttpRoute(
       name: "LocationApiGET",
       routeTemplate: "api/{orgname}/{fleetname}/{vehiclename}/location/{start}",
       defaults: new { controller = "location", start = RouteParameter.Optional }
       constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
   );

   ...

}
like image 114
Derrick Avatar answered Nov 15 '22 17:11

Derrick


You're putting your httpMethod constraint into defaults, but it should be in constraints.

defaults just says what the default values will be if the request doesn't include some or all of them as routing parameters (which in the case of the verb, is meaningless, since every HTTP request always has a verb as part of the protocol). constraints limit the combination of route values that will activate the route, which is what you're actually trying to do.

FYI, for this simple/standard routing, you don't need the [HttpPut] attribute in an API controller either. That's already handled by the HTTP routing which maps the verb to the controller method.

like image 23
Aaronaught Avatar answered Nov 15 '22 17:11

Aaronaught