Keeping ASP.NET MVC action method argument names separate from the query
ASP.NET MVC does a wonderful job of automatically routing and binding your HTTP request data to the action method. But what if you don’t want to use the same argument names in your routes (for example, query strings) and your methods?
There are a few typical scenarios where you might encounter this.
First, if you’re aiming for absolute shortness in URIs, you’ll end up using querystring arguments like “q”, “aqi” etc. (examples from Google). Such names wouldn’t work well in method names, which have more value in being readable.
Second, if your source code and URIs are in a different language, you might end up with a scenario where you have arguments like ?nimi=5, but not wanting to pollute your code with Finnish, you’d want to translate over to “name” (the same in English).
Third, your query string parameters might have names that are downright uncomfortable to use in programming language of your choice. Typical examples in C# include for, in, class and case. Although you can prefix the names with @ sign to escape the reserved words, it’s a bit clumsy.
BindAttribute to the rescue!
There is actually a very good solution to this: You can specify the name of the field to bind to via an attribute. It’s just a bit confusing: You set the name to a field called “Prefix”, although it is actually the full name of the field. Here you go:
- public ActionResult Index
- ([Bind(Prefix = "string")]string text, [Bind(Prefix="integer")]int number)
- {
- return Content(text + ", " + number);
- }
Now you can access this page with an URI like /?string=foo&integer=42, and the resulting output will be “foo, 42”.
November 15, 2010
·
Jouni Heikniemi ·
11 Comments
Tags: ASP.NET MVC · Posted in: .NET, Web
11 Responses
Julius - November 15, 2010
def index
return "%{string}, %{integer}" % {:string => params[:string], :integer => params[:integer]}
end
in RoR. How does ASP.NET MVC handle the parameters? The params hash is handy even if the variables are short and not so meaningful.
Julius - November 15, 2010
Or a better way (in action):
@text = params[:string]
@number = params[:integer]
Is there a reason this won't work in ASP.NET MVC?
Jouni Heikniemi - November 16, 2010
Julius: ASP.NET MVC has a similar facility (i.e. the route values and query string arguments are available as a collection) – so yeah, you could use a syntax similar to what you described.
The notion of passing the information in a method argument is more like syntactic sugar for certain things: First of all, arguments get parsed and strongly typed; if your action method gets an "int number", you can be certain that the input is actually numeric. Moreover, ASP.NET MVC also allows you to declare validation rules (e.g. regexes) for route arguments, thus validating the value domain further ("valid cc numbers" or "positive integers").
All these combined, using the method argument approach cleans the action method code nicely. Admittedly it comes with the cost of spreading the validation rules into different segments of the application, but it's usually a good deal nonetheless, particularly given that similar validation rules often must be applied across different actions anyway.
Julius - November 16, 2010
Looks like Microsoft is in a fine path with the MVC framework.
With rails all parameters come to method as a string. With the use of filters we can be sure the parameters are correct.
Gaurav - May 17, 2016
Awsome artical …it's helped me lot …bookmarking your website
please chack also mine i know it's not batter than your still :D Samsung Galaxy S7 Review screen price Samsung Galaxy S7
cara menanam - May 19, 2016
that's must be great decision
chuan sex tour - August 1, 2016
Im impressed, I have to admit. Seldom do I come across a blog thats both equally educative and amusing, and let me tell you, youve hit the nail on the head. The problem is something which too few people are speaking intelligently about. Im very happy I came across this in my search for something regarding this.
Ayesha - June 16, 2017
I enjoyed reading the article you posted. There were some really great points! I want to encourage you to continue your great writing Skills, have a nice evening!
JATINDAR SINGH - October 17, 2018
Fantastic. Thanks for sharing this valuable information on ASP.NET MVC scenarios to solve the problem.
I hope you will keep sharing more such informative articles.
XEO Home - September 24, 2021
Thanks for sharing such large and most important information shared on this blog post. Most things you shared I have been searching for couple of weeks. Again thanks Dude. Keep it up.
Anupam - May 19, 2022
Amazing information thanks for sharing
Leave a Reply