Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return new HttpNotFoundResult("This doesn't exist");
- }
- }
- using System;
- using System.Net;
- using System.Web;
- using System.Web.Mvc;
- namespace YourNamespaceHere
- {
- /// <summary>An implementation of <see cref="ActionResult" /> that throws an <see cref="HttpException" />.</summary>
- public class HttpNotFoundResult : ActionResult
- {
- /// <summary>Initializes a new instance of <see cref="HttpNotFoundResult" /> with the specified <paramref name="message"/>.</summary>
- /// <param name="message"></param>
- public HttpNotFoundResult(String message)
- {
- this.Message = message;
- }
- /// <summary>Initializes a new instance of <see cref="HttpNotFoundResult" /> with an empty message.</summary>
- public HttpNotFoundResult()
- : this(String.Empty) { }
- /// <summary>Gets or sets the message that will be passed to the thrown <see cref="HttpException" />.</summary>
- public String Message { get; set; }
- /// <summary>Overrides the base <see cref="ActionResult.ExecuteResult" /> functionality to throw an <see cref="HttpException" />.</summary>
- public override void ExecuteResult(ControllerContext context)
- {
- throw new HttpException((Int32)HttpStatusCode.NotFound, this.Message);
- }
- }
- }
- // By Erik van Brakel, with edits from Daniel Schaffer :)
- /// <summary>
- /// returns our standard page not found view
- /// </summary>
- protected ViewResult PageNotFound()
- {
- Response.StatusCode = 404;
- return View("PageNotFound");
- }
- public ActionResult ShowUserDetails(int? id)
- {
- // make sure we have a valid ID
- if (!id.HasValue) return PageNotFound();
- throw new HttpException(404, "Are you sure you're in the right place?");
- using System.Web;
- using System.Web.Mvc;
- namespace YourNamespace.Controllers
- {
- public class BaseController : Controller
- {
- public BaseController()
- {
- ViewBag.MetaDescription = Settings.metaDescription;
- ViewBag.MetaKeywords = Settings.metaKeywords;
- }
- protected new HttpNotFoundResult HttpNotFound(string statusDescription = null)
- {
- return new HttpNotFoundResult(statusDescription);
- }
- protected HttpUnauthorizedResult HttpUnauthorized(string statusDescription = null)
- {
- return new HttpUnauthorizedResult(statusDescription);
- }
- protected class HttpNotFoundResult : HttpStatusCodeResult
- {
- public HttpNotFoundResult() : this(null) { }
- public HttpNotFoundResult(string statusDescription) : base(404, statusDescription) { }
- }
- protected class HttpUnauthorizedResult : HttpStatusCodeResult
- {
- public HttpUnauthorizedResult(string statusDescription) : base(401, statusDescription) { }
- }
- protected class HttpStatusCodeResult : ViewResult
- {
- public int StatusCode { get; private set; }
- public string StatusDescription { get; private set; }
- public HttpStatusCodeResult(int statusCode) : this(statusCode, null) { }
- public HttpStatusCodeResult(int statusCode, string statusDescription)
- {
- this.StatusCode = statusCode;
- this.StatusDescription = statusDescription;
- }
- public override void ExecuteResult(ControllerContext context)
- {
- if (context == null)
- {
- throw new ArgumentNullException("context");
- }
- context.HttpContext.Response.StatusCode = this.StatusCode;
- if (this.StatusDescription != null)
- {
- context.HttpContext.Response.StatusDescription = this.StatusDescription;
- }
- // 1. Uncomment this to use the existing Error.ascx / Error.cshtml to view as an error or
- // 2. Uncomment this and change to any custom view and set the name here or simply
- // 3. (Recommended) Let it commented and the ViewName will be the current controller view action and on your view (or layout view even better) show the @ViewBag.Message to produce an inline message that tell the Not Found or Unauthorized
- //this.ViewName = "Error";
- this.ViewBag.Message = context.HttpContext.Response.StatusDescription;
- base.ExecuteResult(context);
- }
- }
- }
- }
- public ActionResult Index()
- {
- // Some processing
- if (...)
- return HttpNotFound();
- // Other processing
- }
- <div class="content">
- @if (ViewBag.Message != null)
- {
- <div class="inlineMsg"><p>@ViewBag.Message</p></div>
- }
- @RenderBody()
- </div>
- public class HandleNotFoundAttribute : ActionFilterAttribute, IExceptionFilter
- {
- public void OnException(ExceptionContext filterContext)
- {
- var httpException = filterContext.Exception.GetBaseException() as HttpException;
- if (httpException != null && httpException.GetHttpCode() == (int)HttpStatusCode.NotFound)
- {
- filterContext.HttpContext.Response.TrySkipIisCustomErrors = true; // Prevents IIS from intercepting the error and displaying its own content.
- filterContext.ExceptionHandled = true;
- filterContext.HttpContext.Response.StatusCode = (int) HttpStatusCode.NotFound;
- filterContext.Result = new ViewResult
- {
- ViewName = "404",
- ViewData = filterContext.Controller.ViewData,
- TempData = filterContext.Controller.TempData
- };
- }
- }
- }
Add Comment
Please, Sign In to add comment