在 ASP.NET MVC 控制器结果中设置 HTTP 状态不会呈现视图
我有一个自定义的 ActionResult 用于返回某些 HTTP 错误,例如 NotFoundResult 和 ForbiddenResult,它们都派生自 ViewResult。
我将它们用于诸如在操作过程中未在数据库中找到实体时使用 404 进行短路操作的情况。
在这些结果对象中,我将 HTTP 状态设置为适当的数字。当我这样做时,这些 ViewResults 引用的视图不会呈现。我必须将状态保留为 200 OK 才能渲染我的视图。
如何在 ASP.NET MVC 2.0 中设置适当的状态并呈现视图?
I have a custom ActionResult for returning certain HTTP Errors, like NotFoundResult and ForbiddenResult, they all derive from ViewResult.
I use them for instances like short circuiting actions with a 404 if an entity was not found in the database during the course of an action.
Within these result objects, I set the HTTP Status to the appropriate number. When I do that, the view that these ViewResults reference does not render. I have to leave the status as 200 OK in order for my view to be rendered.
How do I set an appropriate status AND render a view in ASP.NET MVC 2.0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请允许我向您建议另一种错误处理方法:
首先创建错误控制器和相应的视图:
在 Global.asax 中定义 Application_Error 方法:
最后抛出适当的异常:
Allow me to suggest you an alternative error handling:
Start by creating an error controller and the corresponding views:
In
Global.asax
define theApplication_Error
method:Finally throw appropriate exceptions: