ASP.NET MVC3 Routing - Same URL for different area

2019-05-31 13:54发布

问题:

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 (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?

回答1:

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"); 
            } 
        }


回答2:

You might look at this...

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