Gia*_*rra 6 c# asp.net asp.net-mvc asp.net-core
我对ASP.NET和MVC一般都很新; 我一直在将ASP.NET MVC应用程序迁移到ASP.NET MVC Core.在前一个框架中,我能够处理以下臭名昭着的错误的HttpException:
HTTP错误404.13 - 未找到
请求过滤模块被配置为拒绝超过请求内容长度的请求.
我知道我可以增加允许的最大长度,默认情况下目前为30MB,但我的目标是向用户提供一个友好的错误页面,解释刚刚发生的事情,而不是增加允许的限制.
在ASP.NET上,我使用Global.asax上的以下代码完成了此操作:
private void Application_Error(object sender, EventArgs e)
{
var ex = Server.GetLastError();
var httpException = ex as HttpException ?? ex.InnerException as HttpException;
if (httpException == null) return;
if (httpException.WebEventCode == WebEventCodes.RuntimeErrorPostTooLarge)
{
//handle the error
Response.Redirect("~/Error/UploadTooLarge"); //Redirect to my custom error page
}
}
Run Code Online (Sandbox Code Playgroud)
经过几个小时的研究,我似乎无法在Asp.Net Core中找到替代方案.我相信我需要在我的Startup.cs配置方法中插入一些中间件来实现自定义错误页面来处理HttpException并重定向它,但我真的迷失了这个问题.
我已经成功地使用以下中间件来查找http错误的自定义错误页面,例如404 - Not Found或403 - Forbiden,在我的configure方法中使用以下内容:
app.UseStatusCodePagesWithReExecute("/Error/StatusCode{0}");
Run Code Online (Sandbox Code Playgroud)
与控制器一起:
public class ErrorController : Controller
{
public IActionResult StatusCode404()
{
return View(viewName: "CustomNotFound");
}
public IActionResult StatusCode403()
{
return View("CustomForbiden");
}
}
Run Code Online (Sandbox Code Playgroud)
和相应的观点.但是,404.13错误(上传太大)不会被我当前的中间件处理.我相信IIS正在呈现错误,因为它不是由Web应用程序处理的.
老帖子,但仍然相关。我的 Core 2.2 MVC 项目包含大型流文件上传,需要妥善处理 404.13(请求大小太大)结果。设置状态代码处理(优雅视图)的常用方法是在 Startup.cs 配置()中加上一个匹配的操作方法:
app.UseStatusCodePagesWithReExecute("/Error/Error", "?statusCode={0}");
Run Code Online (Sandbox Code Playgroud)
和
public IActionResult Error(int? statusCode = null)
{
if (statusCode.HasValue)
{
Log.Error($"Error statusCode: {statusCode}");
if (statusCode == 403)
{
return View(nameof(AccessDenied));
}
if (statusCode == 404)
{
return View(nameof(PageNotFound));
}
}
return View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier
});
}
Run Code Online (Sandbox Code Playgroud)
但由于 404.13 错误是由 IIS 处理的,而不是在 MVC 管道中处理的,因此上面的代码不允许建立优雅的“上传太大”错误视图。为此,我必须忍住并将以下 web.config 添加到我的 Core 2.2 项目中。请注意,删除 404.13 也会删除 404,因此 ErrorController() 代码不再处理 404,因此出现了下面的两个自定义错误处理程序。希望这对某人有帮助!
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<security>
<requestFiltering>
<!-- This will handle requests up to 201Mb -->
<requestLimits maxAllowedContentLength="210763776" />
</requestFiltering>
</security>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="13" />
<remove statusCode="404" />
<error statusCode="404"
subStatusCode="13"
prefixLanguageFilePath=""
path="/Error/UploadTooLarge"
responseMode="Redirect" />
<error statusCode="404"
prefixLanguageFilePath=""
path="/Error/PageNotFound"
responseMode="Redirect" />
</httpErrors>
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
你是对的。IIS 在错误进入您的管道之前捕获该错误。我建议将该httpErrors
模块添加到您的模块中web.config
并将其指向网站上的页面。
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" subStatusCode="13" />
<error statusCode="404"
subStatusCode="13"
prefixLanguageFilePath=""
path="http://yourwebsite.com/path/to/page"
responseMode="Redirect" />
</httpErrors>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3466 次 |
最近记录: |