I have an MVC site that has actions that accept two parameters, something like this:
public ActionResult ListResults(string City, string State)
What I want is to be able to intercept the request and verify the parameters BEFORE it gets to this action. This validation would perform the following:
Ensure both parameters are supplied. If either is missing, fill in with default values
Check the datasource for the existence of that city/state combination. If not found, redirect to 404
If found, ensure that letter casing matches (for example McAllen vs Mcallen). If not matching, replace parameters with correct casing
Right now I'm doing this inside the Action as it loads before I do any processing, redirecting as needed.
This feels like it should happen before the request, but I don't know how. Can someone tell me a) if indeed this can (and should) be extracted out and executed before the action, and b) how to do it
Look into inheriting from the ActionFilterAttribute and overriding OnActionExecuting. This fires before the action and you can simply tag
the action method with the attribute. In the attribute you can have access to route parameters, querystring parameters etc.
However, with that said, instead of doing it this, I would recommend creating a model with both city and state. Then create a custom data annonation validation on that model. That way you don't have to decorate any action method and the validation is then
in the model , close to the data. This will also work better with the model binder and allow you to easily return those validation errors back tot he client without having to write a lot of boilerplate code yourself.
So basically you can do it both ways. The second takes advantage of the existing framework and validation scheme, the first requires you to write a lot of boilerplate code. Personally I would go with the second.
Data annotation validation is a way to decorate your model with validation attributes. For example:
public class Product{
public int ProductId {get;set;}
[Required]
public string Name{get;set;}
}
In this example the Name property is required and asp.net mvc will run validation on it for you. In fact, it will even set up client side validation in javascript automatically. There are a few built in validators, required, stringlength.
hmm that is interesting but I'm not so much validating the input as much as I need to compare the input to a database to ensure that it exists and was spelled with correct casing.
That means I have to do a database call, so I don't think data annotations would be the best way to do this, would it?
An action filter seems like the better option, would you all agree?
From a performance point of view both would identical. In my opinion the gain would be the ability to easily integrate error messages back into the view. But you should look at both and pick whichever one works best for your particular style and which you
think will be easier to maintain in the long run.
the action filter worked masterfully. I was able to both validate the input paramaters aganst the database and even return the instantiated object via the RouteData so it is available to the action.
Pretty good stuff, thanks for your input everyone!
selarom
Member
512 Points
164 Posts
Validating parameters before executing Action?
Mar 29, 2012 07:15 PM|LINK
I have an MVC site that has actions that accept two parameters, something like this:
What I want is to be able to intercept the request and verify the parameters BEFORE it gets to this action. This validation would perform the following:
Right now I'm doing this inside the Action as it loads before I do any processing, redirecting as needed.
This feels like it should happen before the request, but I don't know how. Can someone tell me a) if indeed this can (and should) be extracted out and executed before the action, and b) how to do it
Many thanks!
mvc validation
BrockAllen
All-Star
27534 Points
4907 Posts
MVP
Re: Validating parameters before executing Action?
Mar 29, 2012 07:17 PM|LINK
Checkout ActionFilters in MVC. They allow for generic preprocessing of action methods.
DevelopMentor | http://www.develop.com
thinktecture | http://www.thinktecture.com/
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 07:21 PM|LINK
You can create a custom action filter to do this
http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/understanding-action-filters-cs
Look into inheriting from the ActionFilterAttribute and overriding OnActionExecuting. This fires before the action and you can simply tag the action method with the attribute. In the attribute you can have access to route parameters, querystring parameters etc.
However, with that said, instead of doing it this, I would recommend creating a model with both city and state. Then create a custom data annonation validation on that model. That way you don't have to decorate any action method and the validation is then in the model , close to the data. This will also work better with the model binder and allow you to easily return those validation errors back tot he client without having to write a lot of boilerplate code yourself.
So basically you can do it both ways. The second takes advantage of the existing framework and validation scheme, the first requires you to write a lot of boilerplate code. Personally I would go with the second.
mvc validation
Blog | Twitter : @Hattan
selarom
Member
512 Points
164 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 07:24 PM|LINK
@CodeHobo, can you tell me more about this second option? What is a custom data annotation validation and how do I use it?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 08:04 PM|LINK
Data annotation validation is a way to decorate your model with validation attributes. For example:
public class Product{ public int ProductId {get;set;} [Required] public string Name{get;set;} }In this example the Name property is required and asp.net mvc will run validation on it for you. In fact, it will even set up client side validation in javascript automatically. There are a few built in validators, required, stringlength.
You can read more about data annotation validation here.
http://www.asp.net/mvc/tutorials/older-versions/models-(data)/validation-with-the-data-annotation-validators-cs
In addition to the built in annotations, you can also add your own custom annotation and use that in your model.
Take a look at this post on custom data annotation validation attributes.
http://odetocode.com/blogs/scott/archive/2011/02/21/custom-data-annotation-validator-part-i-server-code.aspx
Blog | Twitter : @Hattan
selarom
Member
512 Points
164 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 08:09 PM|LINK
hmm that is interesting but I'm not so much validating the input as much as I need to compare the input to a database to ensure that it exists and was spelled with correct casing.
That means I have to do a database call, so I don't think data annotations would be the best way to do this, would it?
An action filter seems like the better option, would you all agree?
CodeHobo
All-Star
18647 Points
2647 Posts
Re: Validating parameters before executing Action?
Mar 29, 2012 08:18 PM|LINK
From a performance point of view both would identical. In my opinion the gain would be the ability to easily integrate error messages back into the view. But you should look at both and pick whichever one works best for your particular style and which you think will be easier to maintain in the long run.
Blog | Twitter : @Hattan
selarom
Member
512 Points
164 Posts
Re: Validating parameters before executing Action?
Mar 31, 2012 06:10 PM|LINK
the action filter worked masterfully. I was able to both validate the input paramaters aganst the database and even return the instantiated object via the RouteData so it is available to the action.
Pretty good stuff, thanks for your input everyone!