qqqqqqq
qqqqqqq

Reputation: 2227

Is controller class instance created and a respective instance method is called at each request in ASP.NET Framework?

Are web application type and controller the same things in ASP.NET Framework?

In the CLR via C# there is the following excerpt present:

When future clients make requests of an already running web application, ASP.NET doesn’t create a new AppDomain; instead, it uses the existing AppDomain, creates a new instance of the web application’s type, and starts calling methods. The methods will already be JIT-compiled into native code, so the performance of processing all subsequent client requests is excellent.

Does it mean that controller class instance is created and a respective instance method is called at each request in ASP.NET Framework?

E.g. if I have the following controller:

[RoutePrefix("prefix")]
public class FooController : ApiController
{
    [HttpGet]
    [Route("something/{id}")]
    public string GetSomething(int id)
    {
        return "something";
    }
}

Does it mean that each time I request the prefix/something/47 (any number here instead of the 47 and host before the url should be present) a new instance of the FooController is created and the FooController.GetSomething method is called?

Upvotes: 5

Views: 838

Answers (1)

Tyler Hundley
Tyler Hundley

Reputation: 897

Yes, a new Controller instance is used per request. As stated in this answer to an older question, new instances are used to avoid potential state issues with handling multiple requests.

Upvotes: 4

Related Questions