Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC POST incorrectly returning HTTP 302

I've looked all over and can't find an answer to this. I have a simple test controller in ASP.NET MVC4 set up as follows:

public class TestController {
    [HttpGet]
    public ActionResult Index() {
        MyModel model = new MyModel();
        model.Debug += "GET Method";
        return View(model);
    }

    [HttpPost]
    public ActionResult Post(MyModel model) {
        model.Debug += "POST Method";
        return View("Index", model);
    }
}

The Index view just has a form and a button that POSTs to /Test/Post which should just return HTTP 200 with the Index view. That works as expected on my laptop and on my server. But on the hosting provider I get the following when I perform the POST:

POST /Test/Post returns HTTP 302 Redirect to /Test/Post (What the heck?)
GET /Test/Post returns HTTP 404

How could this possibly happen? Any ideas for troubleshooting this problem?

The only difference that I know of between the environments is that I have .NET 4.5 installed and they have .NET 4.0 installed (and won't install 4.5 for some reason.) The projects target .NET 4 though, so don't think it would matter? Originally I had them targeting 4.5, but changed it after I learned that it isn't installed on the server.

Searching for ASP.NET POSTs returning 302 brings up a lot of questions about redirecting due to logins. But this controller isn't under any sort of restricted folder or [Authorize] attribute.


Update - web.config's

I've tried it with and without <authorization>, same results either way. Here is the system.web, in case this will help:

  <system.web>
    <customErrors mode="Off"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Helpers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Web.WebPages, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/LogOn" timeout="30"/>
    </authentication>
    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="Database" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
      </providers>
    </membership>
    <pages controlRenderingCompatibilityVersion="4.0">
      <namespaces>
        <add namespace="System.Web.Helpers"/>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Web.WebPages"/>
        <add namespace="Microsoft.Web.Mvc"/>
        <add namespace="Tenido.Domain"/>
        <add namespace="Tenido.Web.Mvc.Controllers"/>
      </namespaces>
    </pages>
    <httpModules>
      <add name="ImageResizingModule" type="ImageResizer.InterceptModule"/>
    </httpModules>
  </system.web>
like image 623
DavGarcia Avatar asked Dec 03 '13 04:12

DavGarcia


People also ask

How do I fix Error 302?

You can follow these five steps to fix HTTP 302 errors on your website: Determine whether the redirects are appropriate or not by examining the URLs that are issuing the 302 redirects. Check your plugins to make sure any redirect settings are valid. Ensure that your WordPress URL settings are configured correctly.

Why am I getting 302?

The 302 status code is a redirection message that occurs when a resource or page you're attempting to load has been temporarily moved to a different location. It's usually caused by the web server and doesn't impact the user experience, as the redirect happens automatically.

What is HTTP error 302?

The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the resource requested has been temporarily moved to the URL given by the Location header.

Why 302 instead of 200?

An HTTP 302 Found code means that the client should request the resource temporarily at a different URI. However, the server could be misconfigured. Misconfiguration can improperly respond with 302 Found codes instead of the standard and expected 200 OK code.


2 Answers

For me, we had error redirect configured for custom errors via web.config

<system.web> 
...  

 <customErrors mode="On" defaultRedirect="~/error/errorpage">
...

</customErrors>

It was doing a 302 after an internal error. Setting customErrors mode="Off" bubbled the error up all the way to the browser.

like image 138
sumitkm Avatar answered Sep 30 '22 18:09

sumitkm


Use AllowAnonymous attribute in your controller Method

[AllowAnonymous]
[HttpGet]
public ActionResult Index() {
    MyModel model = new MyModel();
    model.Debug += "GET Method";
    return View(model);
}

Hope it helps

like image 37
Mano1822 Avatar answered Sep 30 '22 19:09

Mano1822