How to get MVC action to return 404

asp.net MvcError HandlingHttp Status-Code-404

asp.net Mvc Problem Overview


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.

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.

What is the best way to go about this?

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

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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

return new HttpNotFoundResult("optional description");

Solution 2 - asp.net Mvc

There are multiple ways to do it,

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

Solution 3 - asp.net Mvc

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

if (notWhatIExpected)
{
    return HttpNotFound();
}

or

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

Solution 4 - asp.net Mvc

Code :

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

Web.config

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

Solution 5 - asp.net Mvc

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

Solution 6 - asp.net Mvc

I've used this:

Response.StatusCode = 404;
return null;

Solution 7 - asp.net Mvc

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();
}

Solution 8 - asp.net Mvc

In NerdDinner eg. Try it

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

Solution 9 - asp.net Mvc

In .NET Core 1.1:

return new NotFoundObjectResult(null);

Solution 10 - asp.net Mvc

I use:

Response.Status = "404 NotFound";

This works for me :-)

Solution 11 - asp.net Mvc

You can also do:

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

Solution 12 - asp.net Mvc

Please try the following demo code:

public ActionResult Test()

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul HilesView Question on Stackoverflow
Solution 1 - asp.net MvcStefan Paul NoackView Answer on Stackoverflow
Solution 2 - asp.net MvcDewfyView Answer on Stackoverflow
Solution 3 - asp.net MvcGone CodingView Answer on Stackoverflow
Solution 4 - asp.net MvcSinan BARANView Answer on Stackoverflow
Solution 5 - asp.net MvcRobert PaulsenView Answer on Stackoverflow
Solution 6 - asp.net MvcWoutView Answer on Stackoverflow
Solution 7 - asp.net MvcgandersView Answer on Stackoverflow
Solution 8 - asp.net MvccemView Answer on Stackoverflow
Solution 9 - asp.net MvcfeedthedogsView Answer on Stackoverflow
Solution 10 - asp.net MvcbrunoView Answer on Stackoverflow
Solution 11 - asp.net MvcChristof MehlstäublerView Answer on Stackoverflow
Solution 12 - asp.net Mvcuser4326800View Answer on Stackoverflow