如何让MVC操作返回404

时间:2022-11-29 14:02:33

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并显示一个错误页面。

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.

我目前只使用返回一个特殊的视图,该视图显示一个针对此操作的友好错误消息,表示没有找到该项。这工作得很好,但理想情况下希望返回404状态码,以便搜索引擎知道该内容不再存在,并可以将其从搜索结果中删除。

What is the best way to go about this?

最好的办法是什么?

Is it as simple as setting Response.StatusCode = 404?

就像设置响应一样简单。StatusCode = 404 ?

12 个解决方案

#1


91  

There are multiple ways to do it,

有多种方法,

  1. You are right in common aspx code it can be assigned in your specified way
  2. 您在公共aspx代码中是正确的,它可以按照您指定的方式分配。
  3. throw new HttpException(404, "Some description");
  4. 抛出新的HttpException(404,“Some description”);

#2


138  

In ASP.NET MVC 3 and above you can return a HttpNotFoundResult from the controller.

在ASP。netmvc 3和以上可以从控制器返回一个HttpNotFoundResult。

return new HttpNotFoundResult("optional description");

#3


51  

In MVC 4 and above you can use the built-in HttpNotFound helper methods:

在MVC 4及以上版本中,您可以使用内置的HttpNotFound helper方法:

if (notWhatIExpected)
{
    return HttpNotFound();
}

or

if (notWhatIExpected)
{
    return HttpNotFound("I did not find message goes here");
}

#4


24  

Code :

代码:

if (id == null)
{
  throw new HttpException(404, "Your error message");//RedirectTo NoFoundPage
}

Web.config

. config

<customErrors mode="On">
  <error statusCode="404" redirect="/Home/NotFound" />
</customErrors>

#5


9  

I've used this:

我用这个:

Response.StatusCode = 404;
return null;

#6


5  

In NerdDinner eg. Try it

在NerdDinner如。试一试

public ActionResult Details(int? id) {
    if (id == null) {
        return new FileNotFoundResult { Message = "No Dinner found due to invalid dinner id" };
    }
    ...
}

#7


5  

If you are working with .NET Core, you can return NotFound()

如果使用。net Core,可以返回NotFound()

#8


4  

None of the above examples worked for me until I added the middle line below:

以上的例子中没有一个对我有效,直到我添加了下面的中线:

public ActionResult FourOhFour()
{
    Response.StatusCode = 404;
    Response.TrySkipIisCustomErrors = true; // this line made it work
    return View();
}

#9


2  

I use:

我使用:

Response.Status = "404 NotFound";

This works for me :-)

这适合我:-)

#10


1  

In .NET Core 1.1:

在。net 1.1核心:

return new NotFoundObjectResult(null);

#11


0  

You can also do:

你也可以做的事:

        if (response.Data.IsPresent == false)
        {
            return StatusCode(HttpStatusCode.NoContent);
        }

#12


-1  

Please try the following demo code:

请尝试以下演示代码:

public ActionResult Test()

{
  return new HttpStatusCodeResult (404,"Not found");
}

#1


91  

There are multiple ways to do it,

有多种方法,

  1. You are right in common aspx code it can be assigned in your specified way
  2. 您在公共aspx代码中是正确的,它可以按照您指定的方式分配。
  3. throw new HttpException(404, "Some description");
  4. 抛出新的HttpException(404,“Some description”);

#2


138  

In ASP.NET MVC 3 and above you can return a HttpNotFoundResult from the controller.

在ASP。netmvc 3和以上可以从控制器返回一个HttpNotFoundResult。

return new HttpNotFoundResult("optional description");

#3


51  

In MVC 4 and above you can use the built-in HttpNotFound helper methods:

在MVC 4及以上版本中,您可以使用内置的HttpNotFound helper方法:

if (notWhatIExpected)
{
    return HttpNotFound();
}

or

if (notWhatIExpected)
{
    return HttpNotFound("I did not find message goes here");
}

#4


24  

Code :

代码:

if (id == null)
{
  throw new HttpException(404, "Your error message");//RedirectTo NoFoundPage
}

Web.config

. config

<customErrors mode="On">
  <error statusCode="404" redirect="/Home/NotFound" />
</customErrors>

#5


9  

I've used this:

我用这个:

Response.StatusCode = 404;
return null;

#6


5  

In NerdDinner eg. Try it

在NerdDinner如。试一试

public ActionResult Details(int? id) {
    if (id == null) {
        return new FileNotFoundResult { Message = "No Dinner found due to invalid dinner id" };
    }
    ...
}

#7


5  

If you are working with .NET Core, you can return NotFound()

如果使用。net Core,可以返回NotFound()

#8


4  

None of the above examples worked for me until I added the middle line below:

以上的例子中没有一个对我有效,直到我添加了下面的中线:

public ActionResult FourOhFour()
{
    Response.StatusCode = 404;
    Response.TrySkipIisCustomErrors = true; // this line made it work
    return View();
}

#9


2  

I use:

我使用:

Response.Status = "404 NotFound";

This works for me :-)

这适合我:-)

#10


1  

In .NET Core 1.1:

在。net 1.1核心:

return new NotFoundObjectResult(null);

#11


0  

You can also do:

你也可以做的事:

        if (response.Data.IsPresent == false)
        {
            return StatusCode(HttpStatusCode.NoContent);
        }

#12


-1  

Please try the following demo code:

请尝试以下演示代码:

public ActionResult Test()

{
  return new HttpStatusCodeResult (404,"Not found");
}