如何实现以下功能?
我的控制器:
if (something == null)
{
//return the view with 404 http header
return View();
}
//return the view with 200 http header
return View();
如何实现以下功能?
我的控制器:
if (something == null)
{
//return the view with 404 http header
return View();
}
//return the view with 200 http header
return View();
写吧
Response.StatusCode = 404;
在返回视图之前。
if (something == null)
{
return new HttpNotFoundResult(); // 404
}
else
{
return new HttpStatusCodeResult(HttpStatusCode.OK); // 200
}
if (something == null)
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return View();
}
//return the view with 200 http header
return View();
你应该设置asTrySkipIisCustomErrors
的属性。Response
true
public ActionResult NotFound()
{
Response.StatusCode = 404;
Response.TrySkipIisCustomErrors = true;
return View();
}
if (something == null)
{
return HttpNotFound();
}
return View();
我会抛出一个 404 异常并创建一个自定义异常过滤器,该过滤器会为 404 错误返回一个未找到的页面。内置HandleError
过滤器不处理 404 错误。
if (something == null)
{
throw new HttpException(404, "Not found")
}
return View();