ASP.NET MVC3路由 - 针对不同地区同一网址 [英] ASP.NET MVC3 Routing - Same URL for different areas
问题描述
我的MVC3项目有称为移动的区域。以下是从桌面浏览器和手机浏览器去我的网站时的行为:
My MVC3 project has an area called Mobile. Following is the behavior when going to my site from a desktop browser and mobile browser:
-
桌面浏览器:网址保持mydomain.com和默认桌面主页显示正确
Desktop browser: URL stays mydomain.com and the default desktop home page is correctly displayed.
手机(iPhone)的浏览器:以mydomain.com/Mobile/Home和移动主页URL变化显示正确
Mobile (iPhone) browsers: URL changes to mydomain.com/Mobile/Home and the mobile home page is correctly displayed.
我想URL留mydomain.com无论它正在从桌面浏览器或移动浏览器中查看。我如何完成的?
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?
推荐答案
尝试使用ActionName过滤器和自定义操作方法选择的移动设备。
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");
}
}
这篇关于ASP.NET MVC3路由 - 针对不同地区同一网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!