views:

259

answers:

1

I have an action method in a controller that needs to do paging. I am passing a page number and pagesize parameter in the querystring. The problem I am having is that the first request I make sets the parameters for all subsequent calls.

public ActionResult GetStuff(string key, int? page, int? pageSize)
{
  // do something magical
}

My route looks like this:

routes.MapRoute("GetStuff", "Stuff/{key}", new {controller = "Stuff", action = "GetStuff"});

When I start debugging my app, I go to the url /Stuff/My_Stuff and the key parameter is correct, and both page and pagesize are null as I would expect. If I make a second call with the url /Stuff/My_Stuff?page=2&pageSize=3 then the values of page and pageSize are still null. If I restart the app, and make my first call include page and pagesize parameters, everything works as I would expect, but then changing those values on subsequent calls retains the values from the first call. In fact, even the key parameter, which is part of my route will keep the same value, even if I change my Url. What am I missing?

I am using IIS 6.1 on Windows Server 2003. I am using extensionless routes. Also, the actual code is in VB.Net, but I don't think that should matter. But for full disclosure, the above code is only representative of my actual code, and not the actual code.

+3  A: 

I had the same problem because I used a DI Container (Castle Windsor) to create my Controllers. The problem arose because of the lifetime settings of Controller classes, because the default lifetime policy in Castle is Singleton (a weird default if you ask me).

It seems that because the Controller instance is only created once in the application's lifetime, the parameters get stuck on their first values.

Setting the lifetime to Transient solved the problem in my case.

Mark Seemann