You can create a route restriction class:
public class UserAgentConstraint : IRouteConstraint { private readonly string _requiredUserAgent; public UserAgentConstraint(string agentParam) { _requiredUserAgent = agentParam; } public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) { return httpContext.Request.UserAgent != null && httpContext.Request.UserAgent.ToLowerInvariant().Contains(_requiredUserAgent); } }
And then force the restriction on one of these routes:
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new {id = RouteParameter.Optional}, constraints: new {customConstraint = new UserAgentConstraint("Chrome")}, namespaces: new[] {"MyNamespace.MVC"} );
Then you can create another route pointing to the controller with the same name in another namespace with or without a different restriction.
elolos
source share