gpt4 book ai didi

asp.net - 如何在 ASP.Net MVC 3 中返回 HttpNotFound() 的 View ?

转载 作者:行者123 更新时间:2023-12-03 07:14:42 25 4
gpt4 key购买 nike

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

编辑/跟进:

我最终使用了这个问题的第二个答案中找到的解决方案,并对 ASP.Net MVC 3 进行了一些细微的调整来处理我的 404:How can I properly handle 404s in ASP.Net MVC?

最佳答案

HttpNotFoundResult 不渲染 View 。它只是将状态代码设置为 404 并返回一个空结果,这对于 AJAX 之类的东西很有用,但如果您想要自定义 404 错误页面,您可以抛出 new HttpException(404, "Not find")将自动渲染 web.config 中配置的 View :

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

关于asp.net - 如何在 ASP.Net MVC 3 中返回 HttpNotFound() 的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4985550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com