Jonathan Small
Jonathan Small

Reputation: 1089

HTTP Error 431 on local machine in a C# MVC application

I'm getting an HTTP Error 431 in a C# MVC application in a View after ajax sends data to my controller. This is the code in my View:

 $.ajax({
        type: "POST",
        url: "@Url.Action("SaveElectionData")",
        dataType: "text",
        data: { 'formData': sendData },
        success: function (msg) {
            alert("back in view, success");
            $("form").submit();
        },
        error: function (req, status, error) {
            alert("back in view, fail");
            $("form").submit();
        }
 });

When I run the application, the ajax sends data to my controller. The controller processes the data and returns a string to the ajax. I get the alert back in view, success. As soon as I click the button to close the alert, I get the 431 error.

When I look at dev tools and look at network, this is what I see:

enter image description here

And when I click on Updated this is what I see:

enter image description here

I am running this on a development machine (a mac) running Visual Studio 2022 and I assume IIS Express (provided by Visual Studio unless its different on a mac as opposed to a windows machine) and I had just cleared out the browser (chrome) cookies. Also, according to the second image, the content length of the request header is only 182.

And this is the code in the view for the form:

@using (Html.BeginForm("Updated", "OnlineEnrollment", FormMethod.Post))
{
}

I do not have any elements in the form since the ajax has already sent the data to the controller.

So why would I be getting this 431 error?

Any ideas?

EDIT 1:

This is the code in my program.cs file looks like:

 namespace AccuRecordV3
 {
    public class Program
    {
       public static void Main(string[] args)
       {
           CreateHostBuilder(args).Build().Run();
       }

       public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.ConfigureKestrel((context, options) =>
                {
                    options.Limits.Http2.MaxRequestHeaderFieldSize = 40960;
                });
            });

    }

 }

Upvotes: 1

Views: 2695

Answers (2)

Chris F Carroll
Chris F Carroll

Reputation: 12380

There are several ways to get a 431 Error, that apply to any web technology not just Asp.Net:

  • A redirect loop
  • Too many, or too big, cookies.
    • (for instance, a hundred Claims in an auth cookie, if each claim is json serialized, could be 30KB)
  • Referer URL too long
  • A combination of the above

In Asp.Net or Asp.Net Core specifically, the default TempData storage is in cookies so using a lot of TempData is way to make your cookies too big.

Options:

  • use less TempData!
  • use the SessionStateTempDataProvider to store state in server-side session instead of cookies.
  • I'd only use the MaxRequestHeadersTotalSize setting if you have some control of the servers, and possibly load balancers, your app get deployed to, and/or you can reasonably argue that large requests aren't going to be a problem.

Upvotes: 0

Lex Li
Lex Li

Reputation: 63264

If you take a look at the network tab, then you will see actually it is Kestrel that returns this 413 response along with the message "Request Header Fields Too Large".

There are quite a few options you can configure there, https://learn.microsoft.com/en-us/aspnet/core/fundamentals/servers/kestrel/options?view=aspnetcore-6.0 and MaxRequestHeadersTotalSize should be the one you need.

Upvotes: 1

Related Questions