Reputation: 1089
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:
And when I click on Updated
this is what I see:
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
Reputation: 12380
There are several ways to get a 431 Error, that apply to any web technology not just Asp.Net:
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:
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
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