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

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

问题描述

我在这里的想法已经不多了。也许你可以建议我使用什么模式或方法。



用户应该能够登录并更改外观他/她的个人资料。
与个性化的区别(AFAIK)是,个性化布局只能看到编辑(him- / herself)。
皮肤的区别,我想,皮肤是预先定义的,但用户应该能够自己更改设置。



我需要能够显示



良好的解决方案是将布局信息保存在数据库表中。此外,它应该缓存我想从数据库加载和使用在CSS。



感谢



strong>编辑:



确定我已经做了一些研究。



在一个View中,从一个DB中获取一个userId(Guid类型)并将其设置为ViewData:
ViewData [ userId] = profile.userId;



该视图使用以下名为Profile.Master的MasterPage,并链接到动态CSS文件:

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

在ProfileController中从DB获取CSS数据并将其返回到动态CSS视图:

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

return View(Style,styles);
}

问题是UserId永远不会传递到动态CSS链接: / p>

参数字典包含方法'System.Web.Mvc.ActionResult Style的非可空类型'System.Guid'的参数'userId'的空条目。



欢迎任何建议。

解决方案

Rob Conery开发的Kona项目中非常整齐的布局自定义功能。当您运行可以在此处找到的源代码时,您将看到布局管理UI允许您更改屏幕上每个组件的位置。



这里使用的方法如下:


  1. 页面呈现时,我们的自定义视图引擎检查哪个母版页应该显示(这样我们可以根据当前设置切换主题)

      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;


  2. 查看引擎使用母版页并渲染由特定控制器操作定义的视图表。例如,我们键入的网站的主要URL指向家庭控制器,索引方法。此方法返回由View引擎渲染的Index.aspx视图。


  3. 当视图引擎渲染Index.aspx页面时,它会启动辅助方法,例如



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


此方法真正负责在aspx页面中为每个div呈现特定的widdgets。这样,如果您的用户更改窗口小部件的布局,它们将在屏幕上正确显示。

  public static void RenderWidgets这个ViewPage pg,Kona.Infrastructure.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.EditNameName:


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);
}
}
}
}

用户能否更改布局? Kona有非常整洁的javascript,它与Ajax一起使用,用户只需将小部件从一个面板拖放到另一个面板,以重新排列布局。


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

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.

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

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.

Thanks

Edit:

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

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

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>

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);
}

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

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.

解决方案

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.

The approach used there is as follows:

  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;
    

  2. 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.

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

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

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);
                }
            }
        }
    }

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天全站免登陆