Been working my way through the MVC MusicStore tutorial and I'm really enjoying it to be honest.
I've been playing with routing and managed to get URLs like "/Store/Rock" working as opposed to "/Store/Browse?Genre=Rock", via this:
Now if I put an invalid genre into the URL it will crash, because the StoreController always assumes that a genre is valid. So I was wondering what is the accepted method of validating GET parameters in MVC?
I was thinking it might be one of the following:
I've been playing with routing and managed to get URLs like "/Store/Rock" working as opposed to "/Store/Browse?Genre=Rock", via this:
Code:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Genre",
"Store/Browse/{genre}",
new { controller = "Store", action = "Browse" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
}
Now if I put an invalid genre into the URL it will crash, because the StoreController always assumes that a genre is valid. So I was wondering what is the accepted method of validating GET parameters in MVC?
I was thinking it might be one of the following:
- Check if genre GET is valid, if not redirect to the Index action and display the /Store/Index View, else carry on.
- Check if genre GET is valid, if not redirect to an "error" View, else carry on.
- Check if genre GET is valid, if not display an error within the /Store/Browse View we are attempting to display.
- Something else.
Last edited: