是否有可能实现ASP.NET MVC的X HTTP的方法,覆盖? [英] Is it possible to implement X-HTTP-Method-Override in ASP.NET MVC?

查看:172
本文介绍了是否有可能实现ASP.NET MVC的X HTTP的方法,覆盖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实施使用ASP.NET MVC和除了奇怪的错误在这里和那里我已经实现我从来电者能够使用的列于开始时,在一起的所有要求的REST的API,原型 X-HTTP-方法,覆盖自定义标题重写HTTP方法。

I'm implementing a prototype of a RESTful API using ASP.NET MVC and apart from the odd bug here and there I've achieve all the requirements I set out at the start, apart from callers being able to use the X-HTTP-Method-Override custom header to override the HTTP method.

我想的是,下面的请求......

What I'd like is that the following request...

GET /someresource/123 HTTP/1.1
X-HTTP-Method-Override: DELETE

...将派遣到实现删除功能,而不是为 GET 功能我控制器的方法该行动(假设有执行行动多种方法,他们都标有不同的的[AcceptVerbs] 属性)。因此,考虑到以下两种方法,我想上述请求分派到第二个:

...would be dispatched to my controller method that implements the DELETE functionality rather than the GET functionality for that action (assuming that there are multiple methods implementing the action, and that they are marked with different [AcceptVerbs] attributes). So, given the following two methods, I would like the above request to be dispatched to the second one:

[ActionName("someresource")]
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetSomeResource(int id) { /* ... */ }

[ActionName("someresource")]
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult DeleteSomeResource(int id) { /* ... */ }

有谁知道这是否可能?而且会是多少工作要做所以...?

Does anybody know if this is possible? And how much work would it be to do so...?

推荐答案

您将无法使用的[AcceptVerbs]属性为,是因为它绑在要求的实际的HTTP动词。幸运的是,的[AcceptVerbs]属性是非常简单的;你可以在<一看到自己的源href=\"http://www.$c$cplex.com/aspnet/SourceControl/changeset/view/21528#266431\">http://www.$c$cplex.com/aspnet/SourceControl/changeset/view/21528#266431.

You won't be able to use the [AcceptVerbs] attribute as-is since it's tied to the request's actual HTTP verb. Fortunately the [AcceptVerbs] attribute is very simple; you can see the source for yourself at http://www.codeplex.com/aspnet/SourceControl/changeset/view/21528#266431.

总之,子类AcceptsVerbsAttribute并覆盖IsValidForRequest()方法。实施将是类似以下内容:

In short, subclass AcceptsVerbsAttribute and override the IsValidForRequest() method. The implementation would be something like the following:

string incomingVerb = controllerContext.HttpContext.Request.Headers["X-HTTP-Method-Override"] ?? controllerContext.HttpContext.Request.Method;
return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);

这篇关于是否有可能实现ASP.NET MVC的X HTTP的方法,覆盖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆