You need to use a custom route contraint that checks to see if the optional parameter is in a format you expect. The reason for this is because you need a way to differentiate between the following urls
http://localhost:1035/Home/
(Go to the home controller and pull up the default view)
and
http://localhost:1035/test/ (Go to the home controller index action with test as the input parameter)
In the past what I have done is limit the optional parameters off the site root to a specific format (numeric codes etc). If you truly want any optional parameter, then you have to adjust for not being able to reference a controller using the first example
above.
What you need to do is create an IsClient Route Cosntraint that only matches the route if it's a client. I would not recommend reading from a database
here because this happens with every request, intead you can load the data on application start and put it in the http cache. Then just read of the cache. Alternatively you can just hard code it, but that would require you to rebuild the application each time
a new client joins (which might be ok for you).
/*This will only allow client1 or client2. Optimally you would have this loaded from a cache.
Do not read from a database here!
*/
public class IsClient : IRouteConstraint
{
public bool Match
(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
)
{
var parameter = values[parameterName].ToString();
if(parameter == "client1"){
return true;
}
if(parameter == "client2"){
return true;
}
return false;
}
}
routes.MapRoute(
"clientweb", // Route name
"{id}", // URL with parameters
new { controller = "Client", action = "Index", id=UrlParameter.Optional } ,
new { id = new IsClient() }
);
Edit: Also, route order matters. This needs to come BEFORE the default route.
kvh
Member
131 Points
108 Posts
MapRoute with default parameter
Mar 26, 2012 10:22 AM|LINK
how to configure MapRoute so that on default page I shouldn't need to write full action path to use parameter? for example currently I can use
http://localhost:1035/Home/Index/test
(test is the optional parameter value of string type)
but
http://localhost:1035/test
gives me an error. I understand that somehow I could fix it with a new MapRoute for default controller. but how could this route look like?
thanks.
ignatandrei
All-Star
135204 Points
21687 Posts
Moderator
MVP
Re: MapRoute with default parameter
Mar 26, 2012 11:04 AM|LINK
MapRoute("test/{action}/{id}",..
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MapRoute with default parameter
Mar 26, 2012 05:25 PM|LINK
You need to use a custom route contraint that checks to see if the optional parameter is in a format you expect. The reason for this is because you need a way to differentiate between the following urls
http://localhost:1035/Home/
(Go to the home controller and pull up the default view)
and
http://localhost:1035/test/
(Go to the home controller index action with test as the input parameter)
In the past what I have done is limit the optional parameters off the site root to a specific format (numeric codes etc). If you truly want any optional parameter, then you have to adjust for not being able to reference a controller using the first example above.
Here's a tutorial on creating route constaints
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-route-constraint-cs
Blog | Twitter : @Hattan
kvh
Member
131 Points
108 Posts
Re: MapRoute with default parameter
Mar 29, 2012 03:35 PM|LINK
thanks. can you please give me a sample of custom route constraint? I have tried creating now a new route
routes.MapRoute(
"clientweb", // Route name
"{id}", // URL with parameters
new { controller = "Client", action = "Index" } // Parameter defaults
);
this works. but now accessing Home controller does not work anymore.
my final goal on the site is to give user a personal page in format http://mysite/clientname
that is why it is optional, as if not provided the default page is displayd.
CodeHobo
All-Star
18647 Points
2647 Posts
Re: MapRoute with default parameter
Mar 29, 2012 03:51 PM|LINK
At the bottom of the page i linked to is a link to the next page which has a custom route constraint example. Here it is
http://www.asp.net/mvc/tutorials/controllers-and-routing/creating-a-custom-route-constraint-cs
Also see more here
http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx
What you need to do is create an IsClient Route Cosntraint that only matches the route if it's a client. I would not recommend reading from a database here because this happens with every request, intead you can load the data on application start and put it in the http cache. Then just read of the cache. Alternatively you can just hard code it, but that would require you to rebuild the application each time a new client joins (which might be ok for you).
/*This will only allow client1 or client2. Optimally you would have this loaded from a cache. Do not read from a database here! */ public class IsClient : IRouteConstraint { public bool Match ( HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection ) { var parameter = values[parameterName].ToString(); if(parameter == "client1"){ return true; } if(parameter == "client2"){ return true; } return false; } } routes.MapRoute( "clientweb", // Route name "{id}", // URL with parameters new { controller = "Client", action = "Index", id=UrlParameter.Optional } , new { id = new IsClient() } );Edit: Also, route order matters. This needs to come BEFORE the default route.
Blog | Twitter : @Hattan