简体   繁体   English

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

[英]ASP.NET MVC3 Routing - Same URL for different areas

My MVC3 project has an area called Mobile. 我的MVC3项目有一个名为Mobile的区域。 Following is the behavior when going to my site from a desktop browser and mobile browser: 以下是从桌面浏览器和移动浏览器访问我的网站时的行为:

  1. Desktop browser: URL stays mydomain.com and the default desktop home page is correctly displayed. 桌面浏览器:URL保留mydomain.com并正确显示默认桌面主页。

  2. Mobile (iPhone) browsers: URL changes to mydomain.com/Mobile/Home and the mobile home page is correctly displayed. 移动(iPhone)浏览器:对mydomain.com/Mobile/Home的URL更改,并正确显示移动主页。

I would like the URL to stay mydomain.com regardless of whether it is being viewed from a desktop browser or a mobile browser. 无论是从桌面浏览器还是移动浏览器查看,我都希望URL保留mydomain.com。 How do I accomplishing that? 我如何实现这一目标?

Try to use ActionName filter and custom action method selector for mobile device. 尝试为移动设备使用ActionName过滤器和自定义操作方法选择器。 Example (copy from 'Pro ASP.NET MVC 2' book, page 351): 示例(从“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"); 
            } 
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM