conditional url routing with asp.net 4 webforms
I want to server mobile and web version of pages on my site without any redirection so that if visitor browse them with PC they would see web version and vice versa.
I can do some media queries and reduce stuff on the page but that is not ideal.开发者_如何学Go
I know i can do it with asp.net mvc, but, project is already half finished and I don't have time to rewrite it.
I thought about using conditional routing but as routes register on application start it didn't look possible. Is there anyway using conditional roting?
I am open to suggestions too.
This isn't an MVC solution, but I know you can do this with the IIS7 rewrite module.
<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>
You could certainly also do this with a custom conditional route in 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() }
);
精彩评论