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();
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();
Just write
Response.StatusCode = 404;
before returning the view.
return HttpNotFound(); does under the covers. It adds a 404 StatusCode and StatusDescription to the current HttpContext Response.return. By the way: Alan Low's answer is exactly the same as mine, and there might be a reason for it being upvoted...if (something == null)
{
return new HttpNotFoundResult(); // 404
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.OK); // 200
}
new EmptyResult() is the same as new HttpStatusCodeResult(HttpStatusCode.OK)if (something == null)
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
//return the view with 200 http header
return View();
return, since the second will return as well.You should set TrySkipIisCustomErrors property of Response as true.
public ActionResult NotFound()
{
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
}
if (something == null)
{
return HttpNotFound();
}
return View();