如何在 ASP.Net MVC 3 中返回 HttpNotFound() 的视图? [英] How to return a view for HttpNotFound() in ASP.Net MVC 3?

查看:28
本文介绍了如何在 ASP.Net MVC 3 中返回 HttpNotFound() 的视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在每次从控制器返回 HttpNotFoundResult 时返回相同的视图?你如何指定这个视图?我猜在 web.config 中配置一个 404 页面可能会起作用,但我想知道是否有更好的方法来处理这个结果.

Is there a way to return the same view every time a HttpNotFoundResult is returned from a controller? How do you specify this view? I'm guessing configuring a 404 page in the web.config might work, but I wanted to know if there was a better way to handle this result.

编辑/跟进:

我最终使用了在这个问题的第二个答案中找到的解决方案,并对 ASP.Net MVC 3 进行了一些微调来处理我的 404:如何在 ASP.Net MVC 中正确处理 404?

I ended up using the solution found in the second answer to this question with some slight tweaks for ASP.Net MVC 3 to handle my 404s: How can I properly handle 404s in ASP.Net MVC?

推荐答案

HttpNotFoundResult 不呈现视图.它只是将状态代码设置为 404 并返回一个空结果,这对 AJAX 之类的东西很有用,但如果你想要一个自定义的 404 错误页面,你可以 throw new HttpException(404, "Not found")将自动在 web.config 中呈现配置的视图:

HttpNotFoundResult doesn't render a view. It simply sets the status code to 404 and returns an empty result which is useful for things like AJAX but if you want a custom 404 error page you could throw new HttpException(404, "Not found") which will automatically render the configured view in web.config:

<customErrors mode="RemoteOnly" redirectMode="ResponseRewrite">
   <error statusCode="404" redirect="/Http404.html" />
</customErrors>

这篇关于如何在 ASP.Net MVC 3 中返回 HttpNotFound() 的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆