.NET MVC:如何实现每个用户不同的页面外观? [英] .NET MVC: How to implement different page appearance per user?

查看:388
本文介绍了.NET MVC:如何实现每个用户不同的页面外观?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里运行的想法。也许你可以建议我用什么模式或方式(S)。

I am running out of ideas here. Maybe you can advice me what pattern or method(s) to use.

用户应该能够登录并修改外观仅作为他/她的个人资料。
与个性化的差异(据我所知)是一种个性化的布局被视为只对编辑器(他/她自己)。
蒙皮之间的区别,我想,就是皮肤是predefined但用户应该能够自行更改设置。

User should be able to log in and change the appearance only for his/her profile. The difference (AFAIK) with personalization is that personalized layout are seen only for the editor (him-/herself). The difference between skinning, I guess, is that Skins are predefined but users should be able to change the settings themselves.

我需要能够显示自定义的布局给大家谁访问author`s页。

I need to be able to display the customized layout to everyone who visit author`s page.

好的解决办法是保持在一个数据库表的布局信息。还应该缓存我想采取关闭加载数据库,并在CSS中使用。

The good solution would be to keep the layout info in a DB table. Also it should be cached I guess to take load off the DB and used in CSS.

感谢

编辑:

好吧,我现在已经做了一些研究。想出了这种想法。

OK I have done some research now. Came up with this kind of idea.

在一个视图从数据库获取用户ID(GUID型),并将其设置为ViewData的:
计算机[用户id] = profile.userId;

In a View get a userId (Guid type) from a DB and set it to the ViewData: ViewData["userId"] = profile.userId;

该视图使用下面的母版被称为Profile.Master,并链接到动态CSS文件:

That View uses the following MasterPage called 'Profile.Master' and links to the dynamic CSS file:

    <link href="<%= Url.Action("Style", "Profile", 
        ViewData["userId"]) %>" rel="stylesheet" type="text/css" />
</head>

在ProfileController可从数据库获取数据的CSS并将其返回到动态CSS查看:

In the ProfileController get the CSS data from DB and return it to the dynamic CSS View:

public ActionResult Style(Guid userId)
{
    var styles = (from s in Db.UserStyleSet.OfType<UserStyle>()
                  where s.aspnet_Users.UserId == userId
                  select s);

    return View("Style", styles);
}

问题是,用户ID不会传递到动态CSS链接:

The problem is that the UserId is never passed to the dynamic CSS link:

参数词典共包含参数非可空类型的'用户id'的System.Guid'的方法无效项System.Web.Mvc.ActionResult风格(的System.Guid)'在'Project.Controllers.ProfileController

The parameters dictionary contains a null entry for parameter 'userId' of non-nullable type 'System.Guid' for method 'System.Web.Mvc.ActionResult Style(System.Guid)' in 'Project.Controllers.ProfileController'.

任何意见是值得欢迎的,谢谢你。

Any advice is welcome, thank you.

推荐答案

非常整洁布局定制功能,你可以通过罗布科纳开发项目科纳找到。当您运行源$ C ​​$ C这你可以找到在这里,你会看到布局管理接口,它允许您更改屏幕上的每个部件的位置。

Very neat layout customization features you can find in Kona project developed by Rob Conery. When you run source code which you can find here, you will see layout management UI which allows you to change the position of each component on the screen.

使用的方法有如下:


  1. 在呈现页面我们定制的视图引擎支票母版页应该present(这样,我们能够根据当前设置来切换主题)

  1. When page is rendered our customized view engine check which master page should present (this way we are able to switch themes based on current settings)

    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) {
    ViewEngineResult result = null;
    var request = controllerContext.RequestContext;
    if (controllerContext.Controller.GetType().BaseType == typeof(KonaController)) {
        var orchardController = controllerContext.Controller as KonaController;
        string template = orchardController.ThemeName;


  • 查看引擎使用母版页并呈现视图这是由特定的控制器操作定义使用路由表解决。例如,我们键入它指出,家庭控制器,指数方法的网站主URL。该方法返回这是由视图引擎渲染的Index.aspx视图。

  • View engine uses master page and renders view which was defined by specific controller action resolved using route tables. For instance, we typed main url of the site which pointed to Home Controller, Index method. This method returned Index.aspx view which was rendered by View engine.

    在视图引擎渲染它推出的辅助方法,如

    While view engine is rendering the Index.aspx page it launches helper methods like

    &下;%this.RenderWidgets(sidebar1); %>。

    <%this.RenderWidgets("sidebar1"); %>.

    这方法是在aspx页面呈现每个每个div具体widdgets忠实地负责。这样,如果你的用户更改他们将部件的布局正确$ psented屏幕页上$。

    This method is truely responsible for rendering specific widdgets per each div in the aspx page. This way, if your user changes the layout of the widgets they will be correctly presented on the screen.

            public static void RenderWidgets(this ViewPage pg,  Kona.Infrastructure.Page page, bool useEditor, string zone) {
            if (page != null) {
                foreach (IWidget widget in page.Widgets.Where(x => x.Zone.Equals(zone, StringComparison.InvariantCultureIgnoreCase))) {
    
                    string viewName = useEditor ? widget.EditorName : widget.ViewName;
    
    
                    if (widget.ViewName != null) {
                        if (widget.IsTyped) {
                            var typedWidget = widget as Widget<IList<Product>>;
                            pg.Html.RenderPartial(viewName, typedWidget);
                        } else {
                            pg.Html.RenderPartial(viewName, widget);
                        }
                    } else if (!string.IsNullOrEmpty(widget.Title)) {
                        pg.Html.RenderPartial("TitleAndText", widget);
    
                    } else {
                        pg.Html.RenderPartial("TextOnly", widget);
                    }
                }
            }
        }
    

    用户如何能够改变布局?科纳具有与Ajax和用户共同使用,只需将&放大器非常整齐的javascript;从一个面​​板控件拖放到另一个重新排列布局

    How user is able to change the layout? Kona has very neat javascript which is used together with Ajax and user simply drag&drop widgets from one panel to another to reorder the layout.

    这篇关于.NET MVC:如何实现每个用户不同的页面外观?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆