ASP.NET MVC - Is IsPostBack still here?

前端 未结 11 2098
我在风中等你
我在风中等你 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: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;
     }
    

提交回复
热议问题