我目前已经开发了一个asp.net MVC3网站,现在我想做一个移动版本的网站。我已经阅读了有关移动jquery和如何在mvc中检测移动设备的内容。但我更想知道如何将web /移动web混合在一起……我会为移动网络提供新的控制器和视图吗?这将意味着大量的代码重复和高维护。
任何涵盖此场景的好教程都会很棒。
非常感谢。
一些很好的链接:
http://www.asp.net/whitepapers/add-mobile-pages-to-your-aspnet-web-forms-mvc-application
http://www.hanselman.com/blog/ABetterASPNETMVCMobileDeviceCapabilitiesViewEngine.aspx
阅读上面的链接很有趣,他们有很好的想法,只需要创建一个移动区域,为移动设备提供新的视图,并调整控制器。还可以为移动设备创建一些自定义css样式,然后可以在移动设备的单独母版页中引用这些样式。
发布于 2012-08-07 21:56:54
我推荐你看看这篇博文(如果你不想/不能使用MVC4):http://brockallen.com/2012/05/24/mobile-support-in-mvc-3/。
在那里,Brock Allen解释了如何通过使用Action Filter在MVC3中使用移动/非移动功能。
基本上,您创建了以下类(假设您是用C#编写的):
public class MobileAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
// is the request a view and is the client device a mobile device
var vr = filterContext.Result as ViewResult;
if (vr != null &&
filterContext.HttpContext.Request.Browser.IsMobileDevice)
{
// determine from the current view what the mobile view name would be
var viewName = vr.ViewName;
if (String.IsNullOrWhiteSpace(viewName)) viewName = (string)filterContext.RouteData.Values["action"];
var fileExtension = Path.GetExtension(viewName);
var mobileViewName = Path.ChangeExtension(viewName, "Mobile" + fileExtension);
// ask MVC is we have that view
var ver = ViewEngines.Engines.FindView(filterContext, mobileViewName, vr.MasterName);
if (ver != null && ver.View != null)
{
ver.ViewEngine.ReleaseView(filterContext, ver.View);
// we do, so tell MVC to use the mobile view instead
vr.ViewName = mobileViewName;
}
}
}
}然后,您只需将[Mobile]添加到也具有移动视图的所有控制器中:
[Mobile]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}现在,您可以分别查看计算机和移动设备:
Views/Home/Index.cshtmlViews/Home/Index.Mobile.cshtml这就是你要做的。现在,MVC将自动向移动设备显示Index.Mobile.cshtml,并向计算机显示Index.cshtml。
发布于 2012-03-30 19:25:11
我建议你看看NerdDinner项目-- http://nerddinner.codeplex.com/ --它展示了你所需要的许多功能(桌面浏览器/移动浏览器)。
发布于 2012-07-10 17:33:57
你可以在这里找到一个小教程,教你如何在一个asp mvc3应用程序中使用asp.net mvc4的移动特性:
http://christopheargento.com/2012/01/14/vues-mobiles-en-asp-net-mvc-3-profitez-de-linnovation-majeure-de-mvc-4-sans-attendre/我知道它是法语的,但基本上你必须将这3个类添加到你的应用程序中,并在你的global.asax文件中添加以下代码:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new CustomViewEngine());
DisplayMode iphoneMode = new DisplayMode("Iphone");
iphoneMode.ContextCondition = o => o.Request.UserAgent.IndexOf("iphone", StringComparison.OrdinalIgnoreCase) > 0;
DisplayModes.Modes.Insert(0, iphoneMode);
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}这样做之后,例如,如果您创建了一个名为index.Mobile.cshtml的视图(您必须遵循此命名约定),如果您使用iPhone打开应用程序,则将显示该视图,而不是原始的index.cshtml。
希望这能对你有所帮助。
干杯。
https://stackoverflow.com/questions/9938927
复制相似问题