IIS error page 404

Asked

Viewed 1,837 times

2

Good afternoon, I developed a system and I have a logic that does not allow unauthorized profiles to access parts of the system that is not allowed for it. What happens is that when trying to access an area that is not allowed, IIS gives a 404(Not Found) error page in response to frustrated attempt. What I wanted to know is: Is there any way I can put a custom error page on IIS, so that when this happens, it shows my custom error page ? 'Cause I get scared about the security of my application.
If anyone can help, I’d be grateful !

  • Is using MVC?

  • Exactly, MVC even.

  • In my application I have customized a class to do this kind of redirect. I created a class by inheriting the Defaultcontrollerfactory class, then overriding this method: Getcontrollerinstance

  • It’s because my problem is, I even have a library that when you try to access a page that doesn’t exist, redirects it to a custom page. There is a detail: Without publishing in IIS, running by VS even, when I try to access an area not allowed by a profile that has no access, it redirects to the login page again, that is, logout. But not on IIS, it gives the page 404. And I didn’t want that, I wanted a page like I did, but on IIS.

2 answers

3


Has.

In your file Web.config, add the following:

<system.web>
    <customErrors mode="On" >
        <error statusCode="404" redirect="~/SeuControllerDe404" />
    </customErrors>
</system.web>

Controller:

public class SeuControllerDe404 : Controller 
{
    [AllowAnonymous]
    public ActionResult Index() 
    {
        return View();
    }
}

View:

<div>Oops! Este endereço não existe.</div>
  • Could you put an example of how to create ? I say: Controller and View ?

  • @Érikthiago See now.

  • Now I understand ! Thanks Gypsy !

1

Hello, all right?

I know this question has already been answered but there is much more to it since, that way, you will not be able to return error 404 in a satisfactory way.

The safest way to do it would be by using <httpErrors> within the <system.webServer>, as in the example below:

<httpErrors errorMode="Custom">
    <remove statusCode="404"/>
    <error statusCode="404" path="/erro404.html" responseMode="File"/>
<httpErrors>

This way you will either return HTTP status 404 or retain the original url of the page.

You can see this solution further explained in this article: http://davidsonsousa.net/pt/post/criando-paginas-de-erro-404-personalizadas-no-aspnet-mvc

  • Man, congratulations. Your post and your answer are excellent ! I’m using a nuget package to do this redirect. But this way is great ! And thanks !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.