Asp.net MVC: when does IIS give 404, and when does it forward it to my application?

I was interested to know the following:

I can determine in IIS what to do with page not founds / 404, as well as in my application, I can put it in my section CustomErrorsor just process it in code.

Now, when I assume that IIS always receives the request first, when it processes 404 for itself, and when it allows you to pass it to my application?

And a side question: can IIS really know if the request is in asp.net MVC 404 because it may or may not be displayed by me through any route?

+3
source share
5 answers

IIS . , , .

, foo.jpg , IIS /jpg. , 404.

. , MVC (.. ), IIS -.

+1

IIS , asp.net MVC 404, ?

, , factory . DefaultController factory, , HttpException 404, IIS . , factory.

, Application_Start

ControllerBuilder.Current.SetControllerFactory(new TestControllerFactory());

MVC:

public class TestControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == null)
        {
            //throw new Exception("Oops!");                               // yellow screen of death
            throw new System.Web.HttpException(404, "Oops not found!"); // bubbles up to IIS
        }
        return base.GetControllerInstance(requestContext, controllerType);
    }
}

http://localhost/MvcApplication1/unmapped , , HttpException 404 , ( HttpException , 404)

, IIS ( VS Dev Server), -.

+1

, , IIS , 404 , ?

ISS, MVC. , HttpException, IIS. , .

, RouteExistingFiles. , , true ( false). , , , , . (. SO, , ).

. Application_Error() Application_EndRequest() Global.asax HttpContext.Current.Response, , .

: IIS , asp.net MVC 404, ?

. MVC , , , , . , . . /SomeController/ActionThatDoesExist , . , , 404 .

, , , , , MVC IIS . Integrated Mode Classic Mode. .

+1

IIS , MVC. , .

, . , . , MVC 404, HTTPException, IIS 404.

Webforms, 404 . , , , - ASP.NET Webforms, 404 .

+1

You want to save execution in your MVC application and, as others have said, usually IIS will first send the request to MVC, and only if MVC passes exception 404 is high enough, IIS will return it and the decision-making process will apply it.

Key: handle 404 in MVC correctly!

0
source

Source: https://habr.com/ru/post/1765637/


All Articles