开发者

ASP.NET MVC3 Routing - Same URL for different areas

My MVC3 project has an area called 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.

  2. Mobile 开发者_JAVA百科(iPhone) browsers: URL changes to mydomain.com/Mobile/Home and the mobile home page is correctly displayed.

I would like the URL to stay mydomain.com regardless of whether it is being viewed from a desktop browser or a mobile browser. How do I accomplishing that?


Try to use ActionName filter and custom action method selector for mobile device. Example (copy from 'Pro ASP.NET MVC 2' book, page 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"); 
            } 
        }


You might look at this...

How to simulate Server.Transfer in ASP.NET MVC?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜