ASP.Net MVC: Call your own Custom Error Page in MVC

There are inbuilt option available from MVC to show the error page incase of any error occurred in the application/website but in some cases we need customized error page to show when any error occur.


1. You need to write code on the global.asax file Application_error method. 

void Application_Error(Object sender, EventArgs e)
{

var exception = Server.GetLastError();
if (exception == null)
return;

int statusCode =(exception.GetType() == typeof(HttpException)) ?
((HttpException)exception).GetHttpCode() :500;

var contextWrapper = new HttpContextWrapper(Context);

var errorObject = new ErrorInfo { HttpStatusCode = statusCode, ExceptionMessage = "Error Occured", IsAjaxRequest = contextWrapper.Request.IsAjaxRequest() };
Response.Redirect(String.Format("~/Error/{0}/?responseText={1}", "ErrorView", JsonConvert.SerializeObject(errorObject)));
}

2. You need create a controller named errorcontroller and create action named index as below. where you need to handle the request based and return the view.

 public ActionResult ErrorView(string responseText)
        {

            var errorModel = JsonConvert.DeserializeObject(responseText, typeof(ErrorInfo));
            ErrorInfo errInfo = (ErrorInfo)errorModel;
            if (errInfo.IsAjaxRequest)
            {
                errInfo.IsAjaxRequest = false;
                return new HttpStatusCodeResult(500, JsonConvert.SerializeObject(errInfo)); // Bad Request
            }
            return View("Index", errorModel);

        }
3. Create a view from the action

@model Models.ErrorInfo
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="content">
    <div class="row">
        <div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
            <div class="row">
                <div class="col-sm-12">
                    <div class="text-center error-box">
                        <h1 class="font-xl text-danger"><i class="glyphicon glyphicon glyphicon-remove-circle"></i> Error @Model.HttpStatusCode</h1>
                        <h2 class="font-xl">Error:@Model.ExceptionMessage</strong>.</h2>
                        <br />
                        <p class="lead semi-bold">
                            <strong>You have experienced a technical error.  We apologize.</strong><br />
                            Please try your request again.
                        </p>
                        <p class="lead semi-bold">
                            <small>
                                If you continue to experience issues, please contact the support desk happy to assist.
                            </small>
                        </p>

                    </div>

                </div>
            </div>
        </div>

    </div>

That's all enjoy your custom error page. also you can play with it to send what ever you need to as error and what need to so to the user what not.

If any question please reach to me.:) Happy Error.....

Comments

Popular posts from this blog

LINQ: Using int.TryParse() within a LINQ where clause

ASP.Net MVC: Configuring asp.net session state in SQL Server.

IIS: Publish Project to remote IIS with Web Deploy.