在 ASP.NET 移动 MVC 应用程序中,浏览器类型应该在哪里隔离?
我正在构建主要针对移动浏览器的 ASP.NET MVC 应用程序。 尽管它也将以较小的容量面向桌面。 (我对 MVC 和移动应用程序都很陌生。)
我想知道在 MVC 应用程序中隔离移动用户与桌面用户的最佳实践是什么。
控制器应该负责检查浏览器类型吗? 或者,这种类型的功能应该为视图保留吗?
如果在视图中选中,则可以 & 母版页应该进行检查吗? 您知道网上有什么好的例子吗?
更新: 我刚刚发现 View 方法的重载,该方法接受指定要使用的母版页的字符串参数。
例如:
public ActionResult Index()
{
if (isMobile())
return View("Index", "Mobile", myObject);
else
return View("Index", myObject);
}
对我来说,这表明微软团队中至少有一些人期望在控制器中实现重大区别(例如移动与桌面)。 (我很可能对此感到非常困惑。)
I am building what will primarily be a mobile browser targeted ASP.NET MVC application. Although it will target the desktop as well in a smaller capacity. (I'm fairly new to both MVC and mobile apps.)
I'm wondering what is the best practice for segregating mobile vs. desktop users in an MVC application.
Should the controller be in charge of checking for browser type? Or, should this type of functionality be reserved for the View?
If checked in the view, can & should a masterpage do the checking? Do you know of any good examples online?
Update:
I just discovered an overload of the View method that accepts a string argument specifying the Masterpage to be used.
For example:
public ActionResult Index()
{
if (isMobile())
return View("Index", "Mobile", myObject);
else
return View("Index", myObject);
}
To me this suggests that at least a few people on the Microsoft team expect major distinctions (such as mobile vs. desktop) to be carried out in the controller. (There's a good chance I'm highly confused about this.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为控制器必须了解平台,因为您可以获得很多不同语言的视图,一些视图用于浏览器(移动),另一个视图是桌面应用程序中的另一个视图,另一个视图可以是Web服务,并且所有视图都可以有不同的需求。
如果你有几个视图,你可以调用带有参数的视图来标记视图的类型:
索引(移动)和索引(桌面):
你可以为你的action()获取一个通用方法,并为每种类型获取另一个操作,
索引-> Index4Mobile 和 Index4浏览器& Index4Desktop
在所有这些方法中,为每个平台准备或做一些特殊的事情,或者具有多个视图的单个操作(1 个用于平台)。
I think the controller must know the plataform, because you can get a lot of views in distint languages, Some view for browsers (mobile) another view in Desktop App, another view can be a web service, and all views can have different needs.
If you have a few views, you can call views with parameters to mark the type of view:
Index(mobile) and Index(Desktop) as it:
You can get a general method for your action() and another action for every type,
Index -> Index4Mobile & Index4Browser & Index4Desktop
And in all of this methods prepare or do something special for every plataform, or a Single Action with multiple views(1 for plataform).
任何处理代码渲染的代码都应该通过 CSS 和 javascript 存在于页面本身上。 您的控制器应该不知道您的数据将如何在屏幕上呈现。 视图也不应该真正了解任何相关信息 - 它们只公开 CSS 将呈现的数据。
您的视图吐出的 HTML 描述了您的数据及其组织方式。 只有 CSS 应该知道如何使其看起来适合渲染它的任何设备。
这个链接充满了JavaScript,应该有助于确定哪个移动设备浏览器正在运行。
Any code dealing with rendering of your code should exist on the page itself via CSS and javascript. Your controllers should know nothing about how your data will be rendered on screen. The views shouldn't really even know anything about it either - they only expose the data that your CSS will render.
The HTML your View spits out describes your data and how it is organized. Only the CSS should know how to make it look appropriate for whatever device is rendering it.
This link, chock full of javascript should help determine which mobile browser is running.