【问题标题】:conditional url routing with asp.net 4 webforms使用 asp.net 4 webforms 的条件 url 路由
【发布时间】:2011-09-03 16:12:19
【问题描述】:

我想在我的网站上提供移动版和网页版页面而不进行任何重定向,这样如果访问者使用 PC 浏览它们,他们会看到网页版,反之亦然。

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

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

我考虑过使用条件路由,但随着路由在应用程序启动时注册,它看起来不太可能。无论如何使用条件轮换?

我也愿意接受建议。

【问题讨论】:

    标签: asp.net routing webforms conditional


    【解决方案1】:

    这不是 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() }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多