处理 ASP.NET MVC 2 应用程序的移动版本的最佳实践方法是什么

发布于 2024-10-30 10:03:04 字数 463 浏览 1 评论 0原文

MVC 的优点在于关注点分离,尤其是来自 Asp.net webforms 世界的关注点分离。

我现在有一个包含控制器、操作、模型和视图的 MVC 站点。

看看我的网站,我可以看到,要提供它的移动版本,我所要做的就是交换它的视图部分,并保持控制器、操作和模型不变。

但是,这种“交换”的最佳方法是什么,特别是在 Asp.net MVC 2 中?

控制器和视图之间不可避免地存在一些耦合。例如,某些操作反映视图的名称,从而按约定进行连接。有时,视图是在返回模型时显式定义的。

有时,控制器中的操作甚至包含基于是否是 jax 调用的条件,以返回不同的视图,例如部分视图。

因此,考虑到这一点,假设我对当前的网站很满意,但现在我想创建它的 iPad 版本。因此它包含用于触摸事件的特殊 js 库,并且视图可能不那么冗长,当然 CSS 也不同。

如何将其构建到我的 MVC 2 项目中?

干杯

The beauty of MVC is the separation of concerns, especially coming from the Asp.net webforms world.

I now have an MVC site with the Controllers, the actions, the model, and the views.

Looking at my site I can see that to serve a mobile version of it, all I have to do is swap out the Views section of it, and keep the Controllers, Actions, and Models untouched.

However, what is the best approach for this "swapping" out, specifically in Asp.net MVC 2?

Invetably there is some coupling between the controllers and the views. For example, certain actions reflect the names of the view, thus wiring by conventions. Sometimes the view is explicitly defined when returning the model.

Sometimes even, the actions in the controller contain conditions based on, is this an jax call, to return different views, like partial views for example.

so, with this in mind, say I'm happy with my current site, but now I want to create, say, an iPad version of it. So it my contain special js libraries for touch events, and the views may be less verbose, and of course the CSS different.

how do I build this into my MVC 2 project?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

独享拥抱 2024-11-06 10:03:04

我想到的一种方法是创建自己的 ViewEngine 并覆盖 FindView,它可以访问控制器上下文,从而访问 HttpContext。您可以使用它来根据用户代理选择不同的视图,使用某种模式,例如附加 _ipad 或其他内容。

简单示例:

Global.asax

protected void Application_Start()
{
     RegisterRoutes(RouteTable.Routes);
     ViewEngines.Engines.Clear();
     ViewEngines.Engines.Add(new CustomViewEngine());
     AreaRegistration.RegisterAllAreas();
}

CustomViewEngine:

public class CustomViewEngine : WebFormViewEngine
{
    protected override IView CreateView(ControllerContext controllerContext, string viewPath,string masterPath)
    {
         if (controllerContext.HttpContext.Request.UserAgent.Contains("ipad"))
        {
             return base.CreateView(controllerContext, viewPath.Replace(".aspx","_ipad.aspx").Replace(".ascx","_ipad.ascx"), masterPath);
         }
         else
         {
             return base.CreateView(controllerContext, viewPath, masterPath);
        }
     }
}

One approach, off the top of my head, would be to create your own ViewEngine and override FindView, it has access to the controller context and therefore the HttpContext. You can use that to choose different views based on the user agent, using some kind of pattern, like appending _ipad or something.

Quick Example:

Global.asax

protected void Application_Start()
{
     RegisterRoutes(RouteTable.Routes);
     ViewEngines.Engines.Clear();
     ViewEngines.Engines.Add(new CustomViewEngine());
     AreaRegistration.RegisterAllAreas();
}

CustomViewEngine:

public class CustomViewEngine : WebFormViewEngine
{
    protected override IView CreateView(ControllerContext controllerContext, string viewPath,string masterPath)
    {
         if (controllerContext.HttpContext.Request.UserAgent.Contains("ipad"))
        {
             return base.CreateView(controllerContext, viewPath.Replace(".aspx","_ipad.aspx").Replace(".ascx","_ipad.ascx"), masterPath);
         }
         else
         {
             return base.CreateView(controllerContext, viewPath, masterPath);
        }
     }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文