1

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.

1
  • You'd have to subclass the ControllerActionInvoker and override the GetParameterValue() method. There's an open request to add support for [DefaultValue] to the next version of the framework, but nobody can make any promises as to whether or not that will actually be implemented. Commented Jun 16, 2009 at 18:16

2 Answers 2

4

Did you try custom filter for this?:

public class DefaultsAttribute : ActionFilterAttribute
{
    public string[] DefaultParams;
    public object[] DefaultValues;

    public DefaultsAttribute(string[] defaultParams, object[] defaultValues)
    {
        DefaultParams = defaultParams;
        DefaultValues = defaultValues;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var params = filterContext.ActionParameters;

        for(int i = 0; i < DefaultParams.Length; i++)
        {
            if (params.Keys.Any(x => x == DefaultParams[i]))
            {
                params[DefaultParams[i]] = DefaultValues[i];
            }
        }
    }
}

Using it (unfortunately CLR does not allow dynamic attribute parameters):

[Defaults(new {"page", "pagesize", "sortby", "sortorder"}, new {1, 10, "ClientInvoiceNumber", "desc"})]
public ActionResult List(int page, int pagesize, string sortby, string sortorder) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

0

you can do this while adding routes to the routes table

routes.MapRoute(
                "<RouteName>",
                "path/to/action{parameter1}",
                new{controller = "<controller name>", action = "<action name>",
                    parameter1= <default value>});

1 Comment

Like I said, I am aware of this, but I would rather not have to create a billion routes for each List() action I have just to set default parameters that vary slightly.

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.