I'm working on an application that was birthed during the Preview 2 days of ASP.NET MVC. It's pretty dirty in places, and I'm trying to clean it up. One problem I'm trying to overcome is overly verbose code.
For example, I have a ton of methods that look exactly like this, but with different default sort parameters. Page and pagesize defaults do not change across the app.
public ActionResult List(int? page, int? pagesize, string sortby, string sortorder)
{
if (string.IsNullOrEmpty(sortby))
sortby = "ClientInvoiceNumber";
if (string.IsNullOrEmpty(sortorder))
sortorder = "desc";
page = page ?? 1;
pagesize = pagesize ?? 10;
...
return View();
}
Now ideally, C# would support something like this:
public ActionResult List(int page = 1, int pagesize = 10, string sortby = "ClientInvoiceNumber", string sortorder = "desc")
But of course, this is not the case.
Where exactly in ASP.NET MVC would I look to write some code to set default parameters on a per-action basis, rather than a per-route basis? Has anyone solved this problem in a clean way, or is there something baked into the framework that I'm simply not aware of? If possible, I'd even like to add any defaulted parameters to the HttpRequestBase parameter collections.
I've thought about this some, but would like to leave the question open-ended as to not artificially guide the answers in a specific direction. I realize that defaults can be set in route definitions, but I'd like to set defaults per-action without having to create a billion routes.