ASP.NET MVC POST incorrectly returns HTTP 302

I looked through everything and can not find the answer to this question. I have a simple test controller in ASP.NET MVC4 configured 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 simply has a form and a button that are sent to / Test / Post, which should simply return an HTTP 200 with the Index view. This works on both my laptop and my server. But on the hosting provider, I get the following when I do POST:

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

How could this happen? Any ideas on how to fix this?

The only difference I know between environments is that I have .NET 4.5 installed and they have .NET 4.0 installed (and for some reason will not install 4.5.) Projects target .NET 4, though, Think it will make a difference? Initially, they were aimed at 4.5, but changed it after I found out that it was not installed on the server.

Finding ASP.NET POST requests returning 302 raises a lot of login redirection questions. But this controller is not under any limited folder or [Authorize] attribute.


Update - web.config

I tried this with and without <authorization> , the same results anyway. Here is the system.web system if that helps:

  <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> 
+10
source share
7 answers

After many years ago, the hosting provider caught my attention that they do not support ASP.NET MVC 4, only up to 3. I am not sure how and why they do not support the new version, but this seems to be the root of the problem.

Needless to say, I switched to a host that supports the latest framework. Now the site is working fine.

+4
source

Use the AllowAnonymous attribute in your controller. Method

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

Hope this helps

+1
source

I had a problem with similiar.

Which helped me remove the <authorization> from web.config.

There is no need for [Authorize] attributes and changed the code from 302 to 404.

0
source

I had a similar problem. After removing the authorization tag from web.config, this worked for me. Not sure about the reason.

0
source

For me, we have redirected an error configured for user errors via web.config

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

He made 302 after an internal error. Setting the mode customErrors = "Off" An error popped up to the browser.

0
source

I had the same problem. It turned out that I published it with Debug setting. Changed it to Release and now everything is fine.

0
source

If you use .AspNet.ApplicationCookie cookie authentication, make sure that the cookie returned for .AspNet.ApplicationCookie (or .AspNet.ApplicationCookie ) matches the domain of the site you are on.

What sometimes happens to me is that the cookie domain is incorrect for the site I work on (due to my outdated clumsy multi-user environment), and then, despite the fact that it has been issued a new auth cookie, the browser cannot send it back during the redirection (because it is a different domain), so it redirects back to the authorization page.

0
source

Source: https://habr.com/ru/post/959214/


All Articles