How to determine if the view is for GET or POST in ASP.NET MVC?

后端 未结 3 742
梦如初夏
梦如初夏 2021-01-01 11:10

MVC use action attributes to map the same view for http get or post:

 [HttpGet] 
 public ActionResult Index()
 {
    ViewBag.Message = \"Message\";
    retur         


        
相关标签:
3条回答
  • 2021-01-01 11:44

    For dot net core it is:

    Context.Request.Method == "POST"

    0 讨论(0)
  • 2021-01-01 12:01
    <% if (System.Web.HttpContext.Current.Request.HttpMethod.ToString() == "GET") { %><!-- This is GET --><% }
       else if (System.Web.HttpContext.Current.Request.HttpMethod.ToString() == "POST")
          { %><!--This is POST--><%}
          else
          { %><!--Something another --><% } %
    
    0 讨论(0)
  • 2021-01-01 12:02

    System.Web.HttpContext.Current.Request.HttpMethod stores current method. Or just Request.HttpMethod inside of view, but if you need to check this, there may be something wrong with your approach.

    Think about using Post-Redirect-Get pattern to form reposting.

    0 讨论(0)
提交回复
热议问题