【问题标题】:ASP.NET MVC3 Routing - Same URL for different areasASP.NET MVC3 路由 - 不同区域的相同 URL
【发布时间】:2011-07-29 14:59:23
【问题描述】:
我的 MVC3 项目有一个名为 Mobile 的区域。以下是从桌面浏览器和移动浏览器访问我的网站时的行为:
桌面浏览器:URL 保持 mydomain.com 并且默认桌面主页正确显示。
移动 (iPhone) 浏览器:URL 更改为 mydomain.com/Mobile/Home,移动主页正确显示。
无论是从桌面浏览器还是移动浏览器查看,我都希望 URL 保留为 mydomain.com。我如何做到这一点?
【问题讨论】:
标签:
c#
asp.net
asp.net-mvc-3
routing
【解决方案1】:
尝试在移动设备上使用 ActionName 过滤器和自定义操作方法选择器。
示例(复制自“Pro ASP.NET MVC 2”一书,第 351 页):
- In Controller define 2 function for desktop & iPhone, they have the same ActionName
[iPhone]
[ActionName("Index")]
public ActionResult Index_iPhone() { /* Logic for iPhones goes here */ }
[ActionName("Index")]
public ActionResult Index_PC() { /* Logic for other devices goes here */ }
- Define [iPhone] action method selector:
public class iPhoneAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext,
MethodInfo methodInfo)
{
var userAgent = controllerContext.HttpContext.Request.UserAgent;
return userAgent != null && userAgent.Contains("iPhone");
}
}