Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate names for querystring parameters

Tags:

c#

asp.net-mvc

Is it somehow possible to set alternate names for querystring parameters in ASP.NET MVC?

I have this simple controller Index action:

public ActionResult Index(color = "")
{
    ...
}

Calling http://mysite.com/mypage/?color=yellow works quite nicely, the color parameter automatically picks up its value "yellow" from the querystring.

But now I would like to have a localized variant of the same page, with “pretty” localized parameters, but still working with the same controller method. Example: http://mysite.com/mypage/?farve=gul. Here I would like “gul” to be passed in as the color parameter to the default Index() ation method.

How do I set mappings for alternate names for querystring parameters?

like image 985
Jakob Gade Avatar asked Aug 02 '13 06:08

Jakob Gade


People also ask

What are URL parameters called?

URL parameters (also known as “query strings”) are a way to structure additional information for a given URL. Parameters are added to the end of a URL after a '? ' symbol, and multiple parameters can be included when separated by the '&' symbol.

What is Querystring parameter?

What are query string parameters? Query string parameters are extensions of a website's base Uniform Resource Locator (URL) loaded by a web browser or client application. Originally query strings were used to record the content of an HTML form or web form on a given page.

What are the different types of query parameters?

There are several types of parameters: header parameters, path parameters, and query string parameters.

What is a query string parameter name?

On the internet, a Query string is the part of a link (otherwise known as a hyperlink or a uniform resource locator, URL for short) which assigns values to specified attributes (known as keys or parameters). Typical link containing a query string is as follows: http://example.com/over/there? name=ferret.


1 Answers

How do I set mappings for alternate names for querystring parameters?

You could write a custom model binder.

So as in every ASP.NET MVC application you start by writing a view model:

public class MyViewModel
{
    public string Color { get; set; }
}

and then a model binder for this model:

public class MyViewModelBinder : IModelBinder
{
    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var query = controllerContext.HttpContext.Request.QueryString;
        var value = query["color"] ?? query["gul"] ?? query["couleur"];
        return new MyViewModel
        {
            Color = value,
        };
    }
}

which will be registered at your Application_Start:

ModelBinders.Binders.Add(typeof(MyViewModel), new MyViewModelBinder());

and now your controller action may take the view model as parameter:

public ActionResult Index(MyViewModel model)
{
    ...
}

Of course you could make the model binder more flexible by using some custom attribute on the property:

public class MyViewModel
{
    [PossibleQueries("color", "gul", "couleur")]
    public string Color { get; set; }
}

and in the model binder read those values and try reading them from the query string until you find one that is not null.

like image 167
Darin Dimitrov Avatar answered Sep 22 '22 01:09

Darin Dimitrov