ASP.NET MVC3 routing - same URL for different areas

My MVC3 project has an area called Mobile. The following is the behavior when going to my site from a desktop browser and mobile browser:

  • Desktop Browser: The URL remains the same as mydomain.com and the default desktop home page.

  • Mobile (iPhone) browsers: Displays URLs at mydomain.com/Mobile/Home and the home page for mobile devices.

I want the url to remain mydomain.com regardless of whether it is viewed from a desktop or mobile browser. How to do it?

+1
source share
2 answers

Try using the ActionName filter and the custom action action selector for the mobile device. Example (copy from Pro ASP.NET MVC 2, p. 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"); } } 
+4
source

Source: https://habr.com/ru/post/1347411/


All Articles