![](/img/trans.png)
[英]Make HttpPost request to an action method in an ASP.NET MVC controller
[英]Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method
我在我的ASP.NET Core MVC
視圖頁面中使用 ajax 調用
我的視圖.cshtml
$.ajax({
processData: false,
contentType: false,
data: new FormData(this),
type: $(this).attr('method'),
url: $(this).attr('action'),
cache: false,
success: function (data) {
$('#mydiv).html(data);
$('#bootstrapModal).modal('show');
Controller 后置方法"
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> MyLongRunningMethod(MyViewModel viewModel)
{
await MyProcess.Longprocess1();
await MyProcess.Longprocess2();
await MyProcess.Longprocess3();
await MyProcess.Longprocess4();
return PartialView("_MyPartialPage");
}
這有效,但僅對長時間運行的過程存在問題。 當進程花費超過 2 分鍾左右時,我在調試模式下收到以下錯誤
我知道這與過期超時有關
在以前版本的 ASP.NET MVC 中,您顯然可以增加 asp.net controller 操作中的超時。
HttpContext.Current.Server.ScriptTimeout = 90000;
然而,這在ASP.NET Core
中不存在
我想增加特定 asp.net controller 的調試和部署超時。
對於生產,我可以通過將 requestTimeout 添加到現有的 httpPlatform 標記,在web.config
中全局設置它。 例如 10 分鍾
<httpPlatform requestTimeout="00:10:00" ....
有人問了一個類似的問題,但是使用CancellationToken
給出的答案但閱讀它,似乎它不能幫助我解決超時問題。
ASP.NET 4
中那樣的每個 controller 超時設置?在aspNetCore
標記上設置RequestTimeout="00:20:00"
並部署站點將導致它不會超時。
問題現在僅在從 Visual Studio 調試模式而不是在發布模式下運行時發生。
注意:在 ASP.NET RTM 中, web.config
的httpPlatform
標記已替換為aspNetCore
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> </handlers> <aspNetCore requestTimeout="00:20:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\\logs\\stdout" forwardWindowsAuthToken="false"/> </system.webServer> </configuration>
但是在 .Net Core 2.0 中,項目中沒有web.config
文件。 它自動生成。
我通過將.UseKestrel(...)
添加到Program.cs
文件中的BuildWebHost
函數解決了這個問題,如下所示:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); })
.Build();
}
就我而言,即使在更新requestTimeout
參數后,我們仍然在 2 分鍾時遇到 502 超時錯誤。 事實證明,紅隼有一個 2 分鍾的保持活動超時,需要被覆蓋:
var host = new WebHostBuilder()
.UseKestrel(o =>
{
o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
}
)
自 .Net Core 3.1 以來,已接受的答案已更改。 在 NET Core 3.1 中,使用以下實現來增加 KeepAliveTimeout:
程序.cs
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureKestrel(options =>
{
options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
});
});
Web.Config(如果不存在,您需要將配置文件添加到您的項目中。)
根據需要在 web.config 上增加 requestTimeout:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
</handlers>
<aspNetCore requestTimeout="00:15:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
</system.webServer>
</configuration>
對我來說這有效:
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); });
適用於 .net 內核 6.0
var builder = WebApplication.CreateBuilder(args);
builder.WebHost.ConfigureKestrel(t =>
{
t.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
});
在 Visual Studio 中,解決方案瀏覽器中不會出現文件 web.config。 我修復了在解決方案中添加文件並更改了“復制到輸出目錄”屬性。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.