sinitram sinitram -4 years ago 82
C# Question

An object of ApiController for each request ASP.NET

Will a new object of API Controller creating after each request to the page?

So I need to know if condition #1 is always true or not?

public class ProductsController : ApiController {
private int _reqState = -1;

public object Get(int id) {
if (_reqState == -1} {} //condition #1

Answer Source

Yes, a controller has a short lifetime, just for this request. After that it is disposed and your value is lost.

If you want to keep some state, you have to use Session, Application or an external storage to save your state.

For example:

private int ReqState
        return (this.HttpContext.Session["ReqState"] as int?).GetValueOrDefault(-1);
        this.HttpContext.Session["ReqState"] = value;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download