如何获得MVC行动来回报404 [英] How to get MVC action to return 404
问题描述
我有取入用于检索某些数据的字符串的操作。如果返回在没有数据这个字符串的结果(也许是因为它已被删除),我想回到404,并显示一个错误页面。
I have an action that takes in a string that is used to retrieve some data. If this string results in no data being returned (maybe because it has been deleted), I want to return a 404 and display an error page.
我目前只使用返回显示特定于这个动作说,该项目未找到一个友好的错误消息的特殊视图。这工作得很好,但在理想情况下想回404状态code使搜索引擎知道这个内容不再存在,并且可以从搜索结果中删除。
I currently just use return a special view that display a friendly error message specific to this action saying that the item was not found. This works fine, but would ideally like to return a 404 status code so search engines know that this content no longer exists and can remove it from the search results.
什么是去最好的方法?
时的那样简单设置Response.Status code = 404?
Is it as simple as setting Response.StatusCode = 404?
推荐答案
有多种方法可以做到这一点,
There are multiple ways to do it,
- 您是对普通的aspx code它可以在你指定的方式分配
-
抛出新的HttpException(404,有些说明);
- You are right in common aspx code it can be assigned in your specified way
throw new HttpException(404, "Some description");
这篇关于如何获得MVC行动来回报404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!