Thursday, June 21, 2018

Custom Error Page in ASP.NET MVC

1. Web.Config

<customErrors mode="On | RemoteOnly"
   defaultRedirect="~/Views/Shared/Error.cshtml">

  <error statusCode="403"
    redirect="~/Views/Shared/UnauthorizedAccess.cshtml" />

  <error statusCode="404"
    redirect="~/Views/Shared/FileNotFound.cshtml" />

</customErrors>

        OR

 <customErrors mode="On" defaultRedirect="~/Home/Error"/>


===========================================
2. App_start/Filter.config

   public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new CustomHandleErrorAttribute());
            filters.Add(new AuthorizeAttribute());

  
         
            filters.Add(new ClearCacheAttribute());
            // filters.Add(new CheckUserCookieAttribute());
            filters.Add(new RequireHttpsAttribute()); 
            filters.Add(new SecuredConnection());
        }
    }
===============================================================
3. Global.asax

  protected override void Application_Start(object sender, EventArgs e)
        {
            base.Application_Start(sender, e);

            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);


            if (!HttpContext.Current.IsDebuggingEnabled)
                BundleTable.EnableOptimizations = true;
        }

==============================
~/Home/Error"/
==============================
public ActionResult Error()
        {
            Exception exception = Server.GetLastError();
            System.Diagnostics.Debug.WriteLine(exception);

            return View();
        }


~/Home/Error.CSHTML
==============================

@model System.Web.Mvc.HandleErrorInfo

@{
    ViewBag.Title = "Error From Config";
}


@Html.ActionLink("Back to Home Page", "Index", new { Controller = "Home" }, new { @class = "regislnk" })


<hgroup class="title">



    <h1 class="error">Error.</h1>
    <h2 class="error">An error occurred while processing your request.</h2>



    @if (Model != null)
    {
        <p>
            <h4 class="error">Exception : @Model.Exception.Message<br />
            </h4>
        </p>
        
   
        <pre>
                @Model.Exception.StackTrace.ToString()
            </pre>
       
    }


</hgroup>

No comments:

Post a Comment

javascript Filter/index off

 var family = [{"name":"Jack",  "age": 26},               {"name":"Jill",  "age"...