Serving Custom Error Pages Dot Net (.Net)

Code Snippets 4 U
using System.Web.Mvc;

namespace DbFirstApproach.Controllers
{
    public class HomeController : Controller
    {
        public void Index()
        {
            HttpContext.Server.ClearError(); // Clears the previous exception.
            HttpContext.Response.Clear(); // clears all content output from the buffer stream
            HttpContext.Response.TrySkipIisCustomErrors = true; // Gets or sets a value that specifies whether IIS 7.0 custom errors are disabled.
            HttpContext.Response.WriteFile("~/Content/404Error.html"); // '~' means home directory 
            HttpContext.Response.StatusCode = 404;
            HttpContext.Response.ContentType = "text/html; charset=utf-8";
            HttpContext.Response.End();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *

twenty two − twelve =