有条件的网址路由与asp.net 4 webforms

问题描述:

我想在我的网站上服务器的移动和网页版本没有任何重定向,以便如果访问者使用PC浏览他们,他们会看到网页版本,反之亦然。有条件的网址路由与asp.net 4 webforms

我可以做一些媒体查询和减少页面上的东西,但这并不理想。

我知道我可以用asp.net mvc来做,但是,项目已经完成了一半,我没有时间重写它。

我想过使用条件路由,但作为路由注册应用程序启动它看起来不可能。反正有没有使用条件循环?

我也乐于接受建议。

这不是一个MVC解决方案,但我知道你可以用IIS7重写模块来做到这一点。

<rewrite> 
    <rules> 
     <rule name="Mobile" stopProcessing="true"> 
      <match url="^(.*)$" /> 
      <conditions logicalGrouping="MatchAny"> 
       <add input="{USER_AGENT}" pattern="iPhone" /> 
      </conditions> 
      <action type="Rewrite" url="Mobile/{R:1}" /> 
     </rule> 
    </rules> 
</rewrite> 

你当然也可以用MVC中的自定义条件路由来做到这一点。

public class MobileConstraint : IRouteConstraint 
{ 
    public MobileConstraint() { } 

    public bool Match(HttpContextBase httpContext, Route route, 
            string parameterName, 
            RouteValueDictionary values, 
            RouteDirection routeDirection) 
    { 
     // add null checking etc 
     return httpContext.Request.UserAgent.Contains("iPhone") 
    } 
} 

context.MapRoute(
    "mobile", 
    "Mobile/{controller}/{action}/{id}", 
    new { action = "Index", controller = "Home", id = UrlParameter.Optional }, 
    new { controller = new MobileConstraint() } 
);