加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何确定视图是否为ASP.NET MVC中的GET或POST?

发布时间:2020-12-16 00:40:15 所属栏目:asp.Net 来源:网络整理
导读:MVC使用动作属性来映射http get或post的相同视图: [HttpGet] public ActionResult Index() { ViewBag.Message = "Message"; return View(); } [HttpPost] public ActionResult Index(decimal a,decimal b,string operation) { ViewBag.Message = "Calculati
MVC使用动作属性来映射http get或post的相同视图:
[HttpGet] 
 public ActionResult Index()
 {
    ViewBag.Message = "Message";
    return View();
 }

 [HttpPost]
 public ActionResult Index(decimal a,decimal b,string operation)
 {
     ViewBag.Message = "Calculation Result:";
     ViewBag.Result = Calculation.Execute(a,b,operation);
     return View();
 }

在MVC视图中,如何确定视图是否为http get或http post?

在意见中是IsPost

@{
     var Message="";
     if(IsPost)
      {
            Message ="This is from the postback";
      }
       else
    {
            Message="This is without postback";
    }
}

解决方法

System.Web.HttpContext.Current.Request.HttpMethod存储当前方法。或者只是Request.HttpMethod内部的视图,但如果你需要检查这一点,你的方法可能会有问题。

考虑使用Post-Redirect-Get模式来形成转发。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读