我正在尝试创建自定义403错误页面,该页面将显示错误中的状态说明。
我正在使用以下代码触发403错误
来自控制器和动作过滤器
控制器调用
return new HttpStatusCodeResult(HttpStatusCode.Forbidden,
"String_Of_Why_This_Request_is_Forbiden")
Actionfilter Call
filterContext.Result = new HttpStatusCodeResult(HttpStatusCode.Forbidden,
"String_Of_Why_This_Request_is_Forbiden")
这两个都被重定向到我的自定义403错误页面。
但是当请求通过我的Web配置和错误控制器时,我丢失了 HTTP状态代码和状态描述。
的Web.Config
<httpErrors errorMode="Custom" existingResponse="Auto">
<remove statusCode="403" subStatusCode="0" />
<error statusCode="403" subStatusCode="0" path="/Error/Forbidden" responseMode="ExecuteURL" />
</httpErrors>
ErrorControllerAction
public ViewResult Forbidden(string errorMessage)
{
Response.StatusCode = 403;
return View("Forbidden", errorMessage);
}
我的假设是我当前的处理只是通过我的错误控制器生成一个新请求。
问题
我怎样才能通过错误控制器将原始状态描述传递给我的视图?
答案 0 :(得分:1)
您可以重定向到控制器操作并转而输入错误消息。
答案 1 :(得分:1)
这是ASP.NET MVC中自定义错误页面的一篇很好的通用文章
https://www.c-sharpcorner.com/UploadFile/618722/custom-error-page-in-Asp-Net-mvc/