如何获得MVC行动来回报404 [英] How to get MVC action to return 404

查看:134
本文介绍了如何获得MVC行动来回报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,


  1. 您是对普通的aspx code它可以在你指定的方式分配

  2. 抛出新的HttpException(404,有些说明);

  1. You are right in common aspx code it can be assigned in your specified way
  2. throw new HttpException(404, "Some description");

这篇关于如何获得MVC行动来回报404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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