ASP.NET MVC3 路由 - 不同区域的相同 URL

本文关键字:区域 URL NET MVC3 路由 ASP | 更新日期: 2023-09-27 17:48:54

我的MVC3项目有一个名为Mobile的区域。以下是从桌面浏览器和移动浏览器转到我的网站时的行为:

  1. 桌面浏览器:URL 保持 mydomain.com,默认桌面主页正确显示。

  2. 移动 (iPhone) 浏览器:URL 更改为 mydomain.com/Mobile/Home,移动主页正确显示。

我希望 URL 保持 mydomain.com,无论是从桌面浏览器还是移动浏览器查看。我该如何做到这一点?

ASP.NET MVC3 路由 - 不同区域的相同 URL

尝试对移动设备使用操作名称筛选器和自定义操作方法选择器。示例(摘自"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"); 
            } 
        }

你可能会看看这个...

如何在 MVC ASP.NET 模拟 Server.Transfer ?