32

How do I achieve the below functionality?

My controler:

if (something == null)
{         
     //return the view with 404 http header
     return View();          
}

  //return the view with 200 http header
  return View();
1
  • 1
    Returning the view with a 404 make no sense. 404 is the status code for page not found. To also return a page is just plain strange. Expect browser inconsistencies if you do this. Commented Oct 12, 2017 at 13:27

6 Answers 6

47

Just write

Response.StatusCode = 404;

before returning the view.

Sign up to request clarification or add additional context in comments.

3 Comments

This is exactly what return HttpNotFound(); does under the covers. It adds a 404 StatusCode and StatusDescription to the current HttpContext Response.
@levelnis The difference is that this command does not end the function like return. By the way: Alan Low's answer is exactly the same as mine, and there might be a reason for it being upvoted...
Thanks for the answer! For this to actually suit my needs, I had to add the following to my web config: <system.webServer> <httpErrors errorMode="Detailed"> <remove statusCode="404"/> </httpErrors> </system.webServer>
17
if (something == null)
{         
   return new HttpNotFoundResult(); // 404
}
else
{
   return new HttpStatusCodeResult(HttpStatusCode.OK); // 200
}

3 Comments

The question is not how to return 404, but how to return the same page with 404 http header.
If you think about it, every request to a web server simply returns a response with a status code. If the status code is 200 there's usually some html with it which we think of as a page, but it's just a status code as far as the server is concerned, whether that's 200, 404, etc.
FYI new EmptyResult() is the same as new HttpStatusCodeResult(HttpStatusCode.OK)
10
if (something == null)
{         
    Response.StatusCode = (int)HttpStatusCode.NotFound;
    return View();          
}

//return the view with 200 http header
return View();

3 Comments

So, why the first return, since the second will return as well.
In this example as is, I agree, only the last return would be needed. However I've made an assumption that there might have been some additional code after the null check which would only be relevant if 'something' wasn't null.
As I've stated on the question returning a view and a 404 is bizarre behaviour. I would not recommend doing this (even if the OP asked for it)
9

You should set TrySkipIisCustomErrors property of Response as true.

public ActionResult NotFound()
{
    Response.StatusCode = 404;
    Response.TrySkipIisCustomErrors = true;
    return View();
}

1 Comment

Best answer for my scenario as it allows you to return an arbitrary view instead of the server taking over and displaying the default 404 response
2
if (something == null)
{         
   return HttpNotFound();
}

return View();

1 Comment

As with the above, you'll note that the View is not returned here.
1

I would throw a 404 exception and create a custom exception filter that return a not found page for 404 errors. The built-in HandleError filter doesn't handle 404 errors.

if (something == null)
{         
   throw new HttpException(404, "Not found")
}

return View();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.