Ho un controller che accetta solo un post su questo URL:Come utilizzare un vincolo personalizzato con HttpMethodConstraint nel routing ASP.NET MVC?
POST http://server/stores/123/products
Il POST dovrebbe essere di tipo di contenuto application/json
, quindi questo è quello che ho nella mia tabella di routing:
routes.MapRoute(null,
"stores/{storeId}/products",
new { controller = "Store", action = "Save" },
new {
httpMethod = new HttpMethodConstraint("POST"),
json = new JsonConstraint()
}
);
dove JsonConstraint
è:
public class JsonConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return httpContext.Request.ContentType == "application/json";
}
}
Quando uso il percorso, ho un 405 Forbidden:
The HTTP verb POST used to access path '/stores/123/products' is not allowed
Tuttavia, se rimuovere il vincolo json = new JsonConstraint()
, funziona benissimo. Qualcuno sa cosa sto sbagliando?
favore potete pubblicare il tuo frammento di jQuery? Ho eseguito alcuni test e mostra che ContentType viene mostrato come "application/xml". –
Sto usando un plugin per Firefox chiamato 'REST Client' per testarlo. –