抛出 HttpException 总是会发回 HTTP 500 错误?
我试图向客户端返回 HTTP 403 错误代码。我读过 HttpException 是完成此任务的最干净的方法,但它对我不起作用。我从这样的页面中抛出异常:
throw new HttpException(403,"You must be logged in to access this resource.");
但是,当 CustomErrors 关闭时,这只会给出标准的 ASP.Net 堆栈跟踪(带有 500 错误)。如果 CustomErrors 已打开,则不会重定向到我设置的在发生 403 错误时显示的页面。我应该忘记 HttpException 而自己设置所有 HTTP 代码吗?我该如何解决这个问题?
我的 Web.Config 的自定义错误部分是这样的:
<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="Forbidden.html" />
</customErrors>
我将得到 GenericErrorPage.html,而不是 Forbidden.html
I'm trying to throw an HTTP 403 error code back at the client. I've read that HttpException is the cleanest way to accomplish this, but it's not working for me. I throw the exception from within a page like this:
throw new HttpException(403,"You must be logged in to access this resource.");
However, this will only give a standard ASP.Net stack trace(with 500 error) when CustomErrors is off. If CustomErrors is on, then this will not redirect to the page I have setup to be displayed when a 403 error occurs. Should I forget about HttpException and instead set all the HTTP codes myself? How do I fix this?
The custom errors part of my Web.Config is this:
<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="Forbidden.html" />
</customErrors>
Instead of getting Forbidden.html, I'll get GenericErrorPage.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要像这样覆盖应用程序错误:
然后您必须添加错误控制器:
最后为错误控制器创建一个视图。我只在 Views/Errors/ 中创建了一个名为 index 的视图。
You need to override Application error like this:
Then you've got to add the errorsController:
And last create a view in for errorsController. I created just one view called index in Views/Errors/.
将此代码包含在 Web.config 文件的 configuration/system.web 元素中:
我设法让它按预期工作。
您可以在这里找到一个很好的教程和示例(示例 3 是正确的):
http://aspnetresources.com/articles/CustomErrorPages
或者您可以使用 Response.Status 来执行此操作:
Asp Classic 返回特定的 http 状态代码
With this code included in the element configuration/system.web of Web.config file:
I managed it to work as expected.
You can find a good tutorial with examples (example 3 is the right one) here:
http://aspnetresources.com/articles/CustomErrorPages
Or you may use Response.Status to do so:
Asp Classic return specific http status code
实际上,我最终创建了自己的漂亮小课程来解决这个问题。它不能处理所有事情,我不确定它是否能与 MVC 配合良好,但它适合我的使用。基本上,它不会依赖 ASP.Net 输出正确的错误页面和错误代码,而是会清除错误,然后进行服务器端传输并显示相应的 Web.Config 错误页面。它还可以识别 customErrors 模式并做出相应的反应。
然后要激活它,只需在 Global.asax 中添加一个小东西即可
I've actually ended up making my own nifty little class to fix this problem. It doesn't handle everything and I'm not sure it plays nice with MVC, but it works for my use. Basically, instead of relying on ASP.Net to output the correct error page and error code, it will clear the error and then do a server-side transfer and display the appropriate Web.Config error page. It also recognizes the customErrors mode and reacts accordingly.
And then to activate it, just add a small thing to Global.asax
我们使用 System.Web.HttpApplication 用于一个应用程序,我发现的最简单的方法是创建一个 System.Net.Http。HttpResponseMessage(指定 System.Net。HttpStatusCode 以及可选的内容,例如 System.Net.Http。StringContent) 并将其放入 System.Web.Http。HttpResponseException。
例如
We're using System.Web.HttpApplication for an app and this simplest way I've found is to create a System.Net.Http.HttpResponseMessage (specifying the System.Net.HttpStatusCode and optionally content like System.Net.Http.StringContent) and throw it in a System.Web.Http.HttpResponseException.
For example