ASP.NET MVC - Is IsPostBack still here?
Asked Answered
S

11

21

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.

Shameful answered 22/4, 2009 at 13:20 Comment(2)
public static bool IsPostBack(this HttpRequestBase request) { return __viewstate-something-something } is probably the closest you'll get. But placing Web Form User Controls in MVC Master Pages might break in future versions of ASP.NET MVC.Cigarette
Why should't we use web forms inside of MVC? I'm using web forms. But I'm not professional. What else can we use other than web forms?Arlindaarline
A
52

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

if (Request.HttpMethod=="POST") { 

}
Asquint answered 14/4, 2011 at 8:3 Comment(5)
Thank you for this, this is the exact way you need to do it in MVC!Luckless
AVOID THIS AT ALL COSTS. Your controller code should be organized by methods. Your HttpGet method ie [HttpGet] should be completely separate than your [HttpPost] method. There are many reasons for this, testability being one, readability, etc. See tvanfosson's answer below.Judsonjudus
still helpful in 2012 +1 for thatMask
What do you mean "avoid this at all costs"? What if you want a javascript flag whether or not to execute a script? There's no other way of doing this...Presbyterial
-1: This doesn't distinguish between postbacks and AJAX posts.Seamaid
J
15

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.

Jonathanjonathon answered 22/4, 2009 at 13:39 Comment(1)
Actually there are still post backs when mixing mvc and webforms pages. See my edit above.Shameful
M
11

You can use this piece of code in Razor

@if(IsPost)
{
//dosomething
}
else
{
//do some other thing
}
Mcauley answered 14/10, 2014 at 15:22 Comment(0)
B
4

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;
 }
Berezina answered 10/12, 2014 at 18:13 Comment(0)
C
3

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

Charente answered 22/4, 2009 at 13:33 Comment(1)
ASP.NET View Engine Views do inherit from Page.Reinforce
H
2

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;
        }
    }
}

Hornbill answered 5/2, 2019 at 14:23 Comment(0)
S
0

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.

Sproul answered 22/4, 2009 at 13:34 Comment(0)
I
0

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

Iselaisenberg answered 22/4, 2009 at 13:45 Comment(0)
W
0

I'm not sure if I understood your question correctly, but on the controller you would have an action that handles the initial GET from the browser and a second action to handle POSTs.

 [AcceptVerbs(HttpVerbs.Post)]
 public ActionResult Create(MyModel model)
 {...}

 public ActionResult Create()
 {...}
Wiley answered 22/4, 2009 at 13:47 Comment(0)
S
0

I would definitely take a look at this blog post by Scott Hanselman where he puts an aspx page in a MVC application.

http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx

Your controllers will not have access to the ViewState property. Even if you did want to handle the problem of __VIEWSTATE, you would have to do some work in order to get it into a usable form in an mvc controller. Good luck on coming up with a conversion strategy, no matter how it works out a lot of people would be interested to know kind of problems you would face in the process.

Stronghold answered 22/4, 2009 at 20:4 Comment(0)
J
0

If you have more than one form in an MVC page, you can add a hidden input within the form with a meaningful ID and test if it has a value. This way you do not need to have two separate handlers (one for get and one for post).

So inf the page and inside the form:

 <input type="hidden" id="testForm" name="testForm" value="1"/>

And in the controller :

if (Request.Form["testForm"] != null)
        { 
        // ACTIONS FOR THE POSTED FORM
        }

Hope it helps!

Jerold answered 5/9, 2014 at 22:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.