Custom 403 error page in ASP.NET MVC 2

Maxsteel

I want to show a custom 403 page in my ASP.NET MVC 2 application. I followed following link. I added following to my config file:

<httpErrors>
      <remove statusCode="403" subStatusCode="-1"/>
      <error statusCode="403" path="/403.htm" responseMode="ExecuteURL"/>
</httpErrors>

I am still seeing the default ASP.NET 403 error page. What's wrong? the default ASP.NET 403 error page

abdulbasit

Add below markup in web.config:

 <customErrors mode="On" defaultRedirect="/error/error">
  <error statusCode="400" redirect="/error/badrequest" />
  <error statusCode="403" redirect="/error/forbidden" />
  <error statusCode="404" redirect="/error/notfound" />
  <error statusCode="414" redirect="/error/urltoolong" />
  <error statusCode="503" redirect="/error/serviceunavailable" />
</customErrors>

Add a view model named ErrorInfo with below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Gunaatita.ViewModel
{
    public class ErrorInfo
    {
        public string Message { get; set; }
        public string Description { get; set; }
    }
}

Create a controller name ErrorController with below code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Gunaatita.ViewModel;

namespace Gunaatita.Controllers
{
    [HandleError]
    public class ErrorController : Controller
    {
        public ActionResult Error()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "An Error Has Occured";
            errorInfo.Description = "An unexpected error occured on our website. The website administrator has been notified.";
            return PartialView(errorInfo);
        }
        public ActionResult BadRequest()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "Bad Request";
            errorInfo.Description = "The request cannot be fulfilled due to bad syntax.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult NotFound()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "We are sorry, the page you requested cannot be found.";
            errorInfo.Description = "The URL may be misspelled or the page you're looking for is no longer available.";
            return PartialView("Error", errorInfo);
        }

        public ActionResult Forbidden()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "403 Forbidden";
            errorInfo.Description = "Forbidden: You don't have permission to access [directory] on this server.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult URLTooLong()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "URL Too Long";
            errorInfo.Description = "The requested URL is too large to process. That’s all we know.";
            return PartialView("Error", errorInfo);
        }
        public ActionResult ServiceUnavailable()
        {
            ErrorInfo errorInfo = new ErrorInfo();
            errorInfo.Message = "Service Unavailable";
            errorInfo.Description = "Our apologies for the temporary inconvenience. This is due to overloading or maintenance of the server.";
            return PartialView("Error", errorInfo);
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}

And update \Views\Shared\Error.cshtml with below markup:

@model Gunaatita.ViewModel.ErrorInfo
@{
    ViewBag.Title = "Problem";
    Layout = "~/Views/Shared/_LayoutSite.cshtml";
}

<div class="middle-container">


    <link rel="stylesheet" href="/Content/css/thankyou.css">
    <!--- middle Container ---->

    <div class="middle-container">
        <div class="paddings thankyou-section" data-moduleid="2050" id="ContactUsPane">
            @if (Model != null)
            {
                <h1>@Model.Message</h1>
                <p>@Model.Description</p>
            }
            else
            {
                <h1>An Error Has Occured</h1>
                <p>An unexpected error occured on our website. The website administrator has been notified.</p>
            }

            <p><a href="/" class="btn-read-more">Go To Home Page</a></p>
        </div>
    </div>
    <!--- middle Container ---->

</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Custom 401 error page for ASP.NET MVC application

ASP.NET MVC custom error page not working correctly

Asp.net MVC Access Custom Error Page Without Authorization

Custom error page for Http error 404.13 ASP.NET Core MVC

Custom error pages in ASP.NET MVC

asp.net Custom error page not working

ASP.NET MVC 4 Custom 404 Page

ASP.NET MVC Custom user fields on every page

asp.net core mvc custom registration page

C# asp.net mvc custom error handling with JSON

Custom error pages on asp.net MVC3

ASP.NET MVC 4 Custom Handle Error

Custom error message on model validation ASP.NET Core MVC

ASP.NET MVC 5 - Error Handle for - 404 Page not found

Redirect to error page from ajax call in ASP.net MVC

ASP.NET MVC SqlException does not redirect to error page

ASP.NET WindowsAuthentication custom 401 Unauthorized error page

403 Error for Custom Login Page Spring Security

Custom 403 error page in django rest framework

Custom 403 Error Page not loading CSS

Custom 403 Error Page in Root Directory

not loading css & jQuery after publish - asp.net mvc (error 403)

Error in asp.net mvc core 2 scaffolding generated Controller

HTTP ERROR : 405(This page isn't working) When I Refresh The Page(ASP.Net Core MVC)

AngularJS show custom error message / page for 403 http error code

Getting 403 Error While Redirecting to Custom Error Page

Error handling in ASP .Net MVC

Redirect user to custom page when s/he is not authorized when using ADFS and ASP.NET MVC

Is there a correct way to add custom Javascript to an ASP.NET MVC 5 page?

TOP Ranking

HotTag

Archive