ASP.NET MVC - Is IsPostBack still here?

前端 未结 11 2076
我在风中等你
我在风中等你 2020-12-09 17:09

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 ou

相关标签:
11条回答
  • 2020-12-09 17:24

    In case anyone is still interested, you can test for a POST from inside an MVC Action Method like this:

    if (Request.HttpMethod=="POST") { 
    
    }
    
    0 讨论(0)
  • 2020-12-09 17:28

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

    0 讨论(0)
  • 2020-12-09 17:28

    The MVC framework doesn't support the classic postback and viewstate used in the Web forms. So, no, you don't have access to the IsPostBack.

    My advice to you is to have two branches: one with the current site where you're adding patches for known errors and another one where you build a new site from scratch. New features should be implemented in this one. I assume that most of your codebase is re-usable in the new site.

    When the new site is ready, put it in production.

    0 讨论(0)
  • 2020-12-09 17:28

    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...

    0 讨论(0)
  • 2020-12-09 17:29

    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;
     }
    
    0 讨论(0)
  • 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;
            }
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题