ASP.NET MVC - Is IsPostBack still here?

2020-02-05 07:10发布

I know, I know, I know. I shouldn't be doing webforms inside of MVC, I completely agree. But, the people who sign my paycheck will not approve a complete conversion of our site to MVC right now. So I am taking incremental steps, page by page, to convert them over while adding new features in MVC.

So my question is how can I access the IsPostBack property from a controller?

Edit: To further clarify, I have a webform user control on my mvc master page which can initiate postbacks. I'm trying to identify these postbacks verses an mvc post. At this point I think I am going to just check the request form keys for a "__viewstate" key and if its found treat it as a postback.

11条回答
forever°为你锁心
2楼-- · 2020-02-05 07:22

I often use this Method (declared on my BaseController class)

 protected bool IsPostBack()
 {
     bool isPost = string.Compare(Request.HttpMethod, "POST", 
        StringComparison.CurrentCultureIgnoreCase) == 0;
     if (Request.UrlReferrer == null) return false;

     bool isSameUrl = string.Compare(Request.Url.AbsolutePath, 
        Request.UrlReferrer.AbsolutePath, 
        StringComparison.CurrentCultureIgnoreCase) == 0;

     return isPost && isSameUrl;
 }
查看更多
Summer. ? 凉城
3楼-- · 2020-02-05 07:24

There is no IsPostBack -- everything is either a POST or GET (or other HTTP verb). You can limit the HTTP verbs that your action allows, i.e., you'll never see a request from a disallowed verb, using the AcceptVerbsAttribute. For example, the following only allows POSTs.

  [AcceptVerbs( HttpVerbs.Post )]
  [ValidateAntiForgeryToken]
  public ActionResult Update( int id )
  {
  }

If you need to have the same action name do both GET/POST and they actually do different things, you can either give them separate signatures or use the ActionNameAttribute to alias one of the actions so the methods can have different names.

  [AcceptVerbs( HttpVerbs.Get)]
  public ActionResult List()
  {
  }

  [AcceptVerbs( HttpVerbs.Post )]
  [ValidateAntiForgeryToken]
  public ActionResult List( string filter, int page, int limit )
  {
  }

OR

  [ActionName( "List" )]
  [AcceptVerbs( HttpVerbs.Get)]
  public ActionResult ListDisplay()
  {
  }

  [AcceptVerbs( HttpVerbs.Post )]
  [ValidateAntiForgeryToken]
  public ActionResult List()
  {
  }

EDIT: Note that I've added the antiforgery token validation to the POST actions. You really should be using this to protect against cross-site scripting attacks.

查看更多
冷血范
4楼-- · 2020-02-05 07:24

Why are you trying to get that value from within a controller? Not sure if this will help you, but you can still use the traditional Request object to get info that was submitted by a form...

查看更多
仙女界的扛把子
5楼-- · 2020-02-05 07:31

For Asp.net Core 2.x you could create an extension method on HttpRequest. Based on @ibirite answer could be something like this:

using System;
using System.Linq;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Extensions;

namespace MyApp
{
    public static class HttpRequestExtensions
    {
        public static bool IsPostBack(this HttpRequest request)
        {
            var currentUrl = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString);
            var referrer = request.Headers["Referer"].FirstOrDefault();

            bool isPost = string.Compare(request.Method, "POST",
               StringComparison.CurrentCultureIgnoreCase) == 0;
            if (referrer == null) return false;

            bool isSameUrl = string.Compare(currentUrl,
               referrer,
               StringComparison.CurrentCultureIgnoreCase) == 0;

            return isPost && isSameUrl;
        }
    }
}

查看更多
家丑人穷心不美
6楼-- · 2020-02-05 07:32

Controllers do not inherit from System.Web.UI.Page. There is no isPostback property.

查看更多
登录 后发表回答