我發現了原因 - HttpMethodConstraint沒有檢查X-HTTP-Method-Override字段,因此,例如在該文章中,HttpMethodConstraint被設置爲只允許「PUT」,但是couurse「POST」被HttpContext返回,所以失敗了。
我寫我自己RouteConstraint,並張貼在這裏,以便其他人可以從我的麻煩學習。
public class HttpVerbConstraint : IRouteConstraint {
public HttpVerbConstraint(params string[] allowedMethods) {
if (allowedMethods == null) {
throw new ArgumentNullException("allowedMethods");
}
this.AllowedMethods = allowedMethods.ToList<string>().AsReadOnly();
}
protected virtual bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
if (httpContext == null) {
throw new ArgumentNullException("httpContext");
}
if (route == null) {
throw new ArgumentNullException("route");
}
if (parameterName == null) {
throw new ArgumentNullException("parameterName");
}
if (values == null) {
throw new ArgumentNullException("values");
}
string method = httpContext.Request["X-HTTP-Method-Override"] ?? httpContext.Request.HttpMethod;
switch (routeDirection) {
case RouteDirection.IncomingRequest:
return AllowedMethods.Any(v =>
v.Equals(method, StringComparison.OrdinalIgnoreCase));
case RouteDirection.UrlGeneration:
string verb = "GET";
if (values.ContainsKey(parameterName))
verb = values[parameterName].ToString();
return AllowedMethods.Any(v =>
v.Equals(verb, StringComparison.OrdinalIgnoreCase));
}
return true;
}
bool IRouteConstraint.Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
return this.Match(httpContext, route, parameterName, values, routeDirection);
}
public ICollection<string> AllowedMethods { get; set; }
}
您是否檢查過github上的RESTful最新更改?也許它在那裏修好了。 – 2010-01-30 18:50:45
只是一個簡單的評論,在HTTP動詞是大小寫敏感的,有事要小心:) – SerialSeb 2010-02-02 09:09:10
人的,你只要回答我的問題。優秀。 – Ruprict 2011-01-03 20:31:01