为什么需要JsonRequestBehavior?为什么Json Request Behavior需要吗?如果我想限制HttpGet请求我的行动我可以用[HttpPost]属性例子:[HttpPost]public JsonResult Foo(){
return Json("Secrets");}// Instead of:public JsonResult Foo(){
return Json("Secrets", JsonRequestBehavior.AllowGet);}为什么[HttpPost]够了吗?为什么框架“困扰”我们JsonRequestBehavior.AllowGet为每一个JsonResult我们有。如果我想拒绝GET请求,我将添加HttpPost属性。
3 回答
慕哥9229398
TA贡献1877条经验 获得超6个赞
public class AllowJsonGetAttribute : ActionFilterAttribute{ public override void OnResultExecuting(ResultExecutingContext filterContext) { var jsonResult = filterContext.Result as JsonResult; if (jsonResult == null) throw new ArgumentException("Action does not return a JsonResult, attribute AllowJsonGet is not allowed"); jsonResult.JsonRequestBehavior = JsonRequestBehavior.AllowGet; base.OnResultExecuting(filterContext); }}
[AllowJsonGet]public JsonResult MyAjaxAction(){ return Json("this is my test");}
Cats萌萌
TA贡献1805条经验 获得超9个赞
[HttpPost] public JsonResult amc(){}
public JsonResult amc(){}
public JsonResult amc(){ return Json(new Modle.JsonResponseData { Status = flag, Message = msg, Html = html }, JsonRequestBehavior.AllowGet);}
- 3 回答
- 0 关注
- 932 浏览
添加回答
举报
0/150
提交
取消