Ответ 1
Просто напишите
Response.StatusCode = 404;
перед возвратом вида.
Как я могу достичь ниже перечисленных функций?
Мой контроллер:
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();
Вы должны установить TrySkipIisCustomErrors
свойство 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();