问题描述
我尝试将没有对不同页面进行自动化的用户从我的应用重定向到自定义错误页面:CustomErr/Forbidden_403
- 控制器(具有 razorview)。我还在我的 <customErrors>
中配置了 Web.config
但我也需要 <httpErrors>
因为出于某种原因,当我发布到服务器时,这个 403 错误没有被覆盖(更多它显示 404 不是发现错误而不是 403)。
我需要一个视图的相对路径:path="~/CustomErr/Forbidden_403"
,因为我的服务器有多个应用程序,每个应用程序都有自己的文件夹。
我目前看到的:
- 如果我添加带有或不带有相对路径(“~”)的
subStatusCode
:<error statusCode="403" subStatusCode="14" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
则浏览器返回 HTTP 403 错误页面(“网站拒绝显示此网页”) - 如果我删除
subStatusCode
:<error statusCode="403" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
,则浏览器会显示 HTTP 404 错误(“找不到网页”) - 如果我删除
subStatusCode
和相对路径(“~”):<error statusCode="403" path="/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
那么浏览器会返回 HTTP 403 错误页面(“网站拒绝显示此网页”) - 我还有一个
PageNotFound_404
控制器和错误定义在web.config
中,如果我在浏览器中写下错误的 URL,它可以正常工作。 - 自定义错误不仅仅针对 403 Forbidden 显示 => 我用户返回
new HttpStatusCodeCresult(HttpStatusCOde.Forbidden)
以将此 403 代码抛出到服务器。
Web.config
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<remove statusCode="404" />
<error statusCode="403" path="~/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
<error statusCode="404" path="~/CustomErr/NotFound_404" responseMode="ExecuteURL" />
</httpErrors>
我需要从 IIS 配置它吗?
解决方法
您应该删除 subStatusCode 和相对路径("~"),并且您的 path
的值的结尾不应该是 html 或 htm 吗?下面的代码对我有用。
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404"/>
<error statusCode="404" path="/Errors/404.html" responseMode="ExecuteURL"/>
</httpErrors>
,
我最终做的是覆盖 Web.config
中来自 Web.Release.config
的路径
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="403" />
<remove statusCode="404" />
<error statusCode="403" path="/myAppFolder/CustomErr/Forbidden_403" responseMode="ExecuteURL" />
<error statusCode="404" path="/myAppFolder/CustomErr/NotFound_404" responseMode="ExecuteURL" />
</httpErrors>
还有所有带有控制器 url 的 ajax js 文件,我将 url 从 url:"/Controller/Action"
修改为 url:"Controller/Action"
- 它们会自动计算相对路径。