限制每个操作的可用 HTTP 动词是一种好习惯吗?我的代码在没有[HttpGet]
, [HttpPost]
, [HttpPut]
, 或[HttpDelete]
装饰每个动作的情况下更干净,但它也可能不那么健壮或安全。我没有在许多教程或示例代码中看到这样做,除非明确需要动词,例如有两个“创建”操作,其中 GET 版本返回一个新表单,而 POST 版本插入一个新记录。
问问题
2822 次
3 回答
3
就我个人而言,我尝试尊重RESTful 约定并指定 HTTP 动词,但不会修改服务器上任何状态的 GET 操作,因此允许使用任何 HTTP 动词调用它们。
于 2011-04-28T18:34:29.350 回答
1
是的,我认为将您的操作限制在它应该处理的适当 HTTP 方法是一个很好的做法,这将阻止错误请求进入您的系统,降低可能攻击的有效性,改进代码文档,强制执行 RESTful设计等
是的,使用[HttpGet]
, [HttpPost]
.. 属性会使您的代码更难阅读,特别是如果您还使用其他属性,例如[OutputCache]
,[Authorize]
等。
我对 custom 使用了一个小技巧IActionInvoker
,而不是使用属性,我将 HTTP 方法添加到操作方法名称中,例如:
public class AccountController : Controller {
protected override IActionInvoker CreateActionInvoker() {
return new HttpMethodPrefixedActionInvoker();
}
public ActionResult GetLogOn() {
...
}
public ActionResult PostLogOn(LogOnModel model, string returnUrl) {
...
}
public ActionResult GetLogOff() {
...
}
public ActionResult GetRegister() {
...
}
public ActionResult PostRegister(RegisterModel model) {
...
}
[Authorize]
public ActionResult GetChangePassword() {
...
}
[Authorize]
public ActionResult PostChangePassword(ChangePasswordModel model) {
...
}
public ActionResult GetChangePasswordSuccess() {
...
}
}
请注意,这不会更改操作名称,仍然是LogOn
、LogOff
、Register
等。
这是代码:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
public class HttpMethodPrefixedActionInvoker : ControllerActionInvoker {
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName) {
var request = controllerContext.HttpContext.Request;
string httpMethod = request.GetHttpMethodOverride()
?? request.HttpMethod;
// Implicit support for HEAD method.
// Decorate action with [HttpGet] if HEAD support is not wanted (e.g. action has side effects)
if (String.Equals(httpMethod, "HEAD", StringComparison.OrdinalIgnoreCase))
httpMethod = "GET";
string httpMethodAndActionName = httpMethod + actionName;
ActionDescriptor adescr = base.FindAction(controllerContext, controllerDescriptor, httpMethodAndActionName);
if (adescr != null)
adescr = new ActionDescriptorWrapper(adescr, actionName);
return adescr;
}
class ActionDescriptorWrapper : ActionDescriptor {
readonly ActionDescriptor wrapped;
readonly string realActionName;
public override string ActionName {
get { return realActionName; }
}
public override ControllerDescriptor ControllerDescriptor {
get { return wrapped.ControllerDescriptor; }
}
public override string UniqueId {
get { return wrapped.UniqueId; }
}
public ActionDescriptorWrapper(ActionDescriptor wrapped, string realActionName) {
this.wrapped = wrapped;
this.realActionName = realActionName;
}
public override object Execute(ControllerContext controllerContext, IDictionary<string, object> parameters) {
return wrapped.Execute(controllerContext, parameters);
}
public override ParameterDescriptor[] GetParameters() {
return wrapped.GetParameters();
}
public override object[] GetCustomAttributes(bool inherit) {
return wrapped.GetCustomAttributes(inherit);
}
public override object[] GetCustomAttributes(Type attributeType, bool inherit) {
return wrapped.GetCustomAttributes(attributeType, inherit);
}
public override bool Equals(object obj) {
return wrapped.Equals(obj);
}
public override int GetHashCode() {
return wrapped.GetHashCode();
}
public override ICollection<ActionSelector> GetSelectors() {
return wrapped.GetSelectors();
}
public override bool IsDefined(Type attributeType, bool inherit) {
return wrapped.IsDefined(attributeType, inherit);
}
public override string ToString() {
return wrapped.ToString();
}
}
}
于 2011-04-29T16:34:50.110 回答
0
您不需要指定 HttpGet,所有其他您都需要
于 2011-04-28T14:45:06.080 回答