5

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?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

That is helpful. Thank you. I will try to accept your answer asap.
this article can also help you understand it more - hogaf.github.io/archive/2014/11/13/….

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.