Asp.Net MVC HttpPost用法

时间:2023-03-09 22:24:46
Asp.Net MVC  HttpPost用法

一个Action只能用一个http 特性,例如:HttpPost 不能与HttpGet 或者多个HttpPost重复使用,否则会出错

也可以用 [AcceptVerbs("put","get","post")]来表示一个Action可以共用多个请求。只要是包含了这个请求的,都可以调用此Action

HttpPost 等可以和 AcceptVerbs 特性共用

比如:

[HttpPost]

[AcceptVerbs("put","get","post")]

public ActionResult Index()

{

return View();

}

这样是可以的,但实际上他只是当Post提交的时候才进入此Action(或者此Action才能被调用)

把[AcceptVerbs("put","get","post")]改成: [AcceptVerbs(HttpVerbs.Post)]与[HtppPost]共用也是可以的实际上效果一样

自定义Http特性:

public class ZiDingYiAttribute : ActionMethodSelectorAttribute

{

private static readonly AcceptVerbsAttribute _innerAttribute = new AcceptVerbsAttribute(HttpVerbs.Post);

public ZiDingYiAttribute()

{

}

public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)

{

return _innerAttribute.IsValidForRequest(controllerContext, methodInfo);

}

}

使用:

[ZiDingYi]

public ActionResult Index()

{

return View();

}

这样就可以自定义一个Http特性