1
我創造了這個自定義路由類在ASP.NET MVC:重定向當用戶使用IE
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);
}
}
而且在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")
}
}
上面的代碼工作prefectly,但當用戶使用IE時,我得到一個404錯誤。我想重定向到自定義頁面。我不想在Web.Config
文件中使用自定義錯誤,因爲我的錯誤僅適用於IE。如何做到這一點?
感謝您的建議。