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

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

问题描述

有没有一种方法,以每次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解决方案:的如何正确处理404在ASP.Net MVC?

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 不渲染视图。它只是设置状态code至404,并返回一个空的结果是对于像AJAX有用的,但如果你愿意,你可以自定义404错误页面抛出新的HttpException(404,未找到 ),它会自动呈现在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>

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

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