Asp.Net MVC HttpPost用法

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特性

          

 

posted @ 2018-02-02 18:26 破帽遮颜过闹市 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值