I created this Custom Route Class in ASP.NET MVC:
public class UserAgentConstraint:IRouteConstraint {
private 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.Contains(RequiredUserAgent);
}
}
And in Global.asax.cs:
public static void RegisterRoutes(RouteCollection routes) {
routes.MapRoute("myRoute2", "{controller}/{action}/{Id}",
new { controller = "home", action = "index", Id = UrlParameter.Optional }, new {
customConstriant=new UserAgentConstraint("IE")
}
}
The above code works prefectly, but when the user uses IE, I get a 404 Error. I want to redirect to a custom Page. I dont want to use a Custom Error in the Web.Config file because my error is only for use in IE. How can one do this?
Thanks in your advice.
a better way of doing this is using ActionFilter.
you can apply it in Controller level like this :
hope this help.good luck.