Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What your doing seems valid. Although I have some doubts whether I would go to those lengths.</p> <p><strong>Correction</strong></p> <p>I had said</p> <blockquote> <p>If you hadn't implemented this this additional method then the routing would have failed and would have normally returned a 404. I would be tempted to leave it with this behavior.</p> </blockquote> <p>But you are correct:</p> <p>To get it to operate as above i.e. default to a 404, you would need the following route configurations:</p> <pre><code> config.Routes.MapHttpRoute( name: "DefaultCollectionApi", routeTemplate: "api/{controller}", defaults: new { }, constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) } ); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { }, constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get, HttpMethod.Put, HttpMethod.Delete) } ); </code></pre> <p><strong>To improve your existing approach slightly</strong></p> <p>However, if you did feel the need to start policing the combination of verbs and routes then it may be worth moving this logic into an ActionFilterAttribute. </p> <p>If you add this route at the top then it will en Assuming you use <code>"id"</code> by convention for all routes you could quickly put together a filter that looked for a value in <code>RouteValues</code> with the key of <code>id</code> and raise a 400 exception.</p> <pre><code>public class ValidVerbAndRouteAttribute : ActionFilterAttribute { public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { object id; if (actionExecutedContext.ActionContext.ActionArguments.TryGetValue("id", out id) &amp;&amp; actionExecutedContext.Request.Method == HttpMethod.Post) { throw new HttpResponseException(HttpStatusCode.BadRequest); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload