我的想法已经用完了。也许你可以建议我使用什么模式或方法。
用户应该能够登录并更改他/她的个人资料的外观。 与个性化的区别(AFAIK)是个性化布局只对编辑(他/她自己)可见。 我猜想,皮肤之间的区别在于皮肤是预定义的,但用户应该能够自己更改设置。
我需要能够向访问作者页面的每个人显示自定义布局。
好的解决方案是将布局信息保存在数据库表中。我想它也应该被缓存以减轻数据库的负担并在 CSS 中使用。
谢谢
编辑:
好的,我现在已经做了一些研究。想出了这样的想法。
在 View 中从数据库获取用户 ID(Guid 类型)并将其设置为 ViewData: ViewData["userId"] = profile.userId;
该 View 使用以下名为“Profile.Master”的 MasterPage 并链接到动态 CSS 文件:
<link href="<%= Url.Action("Style", "Profile",
ViewData["userId"]) %>" rel="stylesheet" type="text/css" />
</head>
在 ProfileController 中从数据库中获取 CSS 数据并将其返回给动态 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);
}
问题是 UserId 永远不会传递给动态 CSS 链接:
参数字典包含“Project.Controllers.ProfileController”中方法“System.Web.Mvc.ActionResult Style(System.Guid)”的不可为空类型“System.Guid”的参数“userId”的空条目.
欢迎任何建议,谢谢。
最佳答案
您可以在 Rob Conery 开发的 Kona 元素中找到非常简洁的布局自定义功能。当您运行 you can find here 的源代码时,您将看到布局管理 UI,它允许您更改每个组件在屏幕上的位置。
那里使用的方法如下:
当页面呈现时,我们的自定义 View 引擎会检查应该呈现哪个母版页(这样我们就可以根据当前设置切换主题)
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;
View 引擎使用母版页并呈现由使用路由表解析的特定 Controller 操作定义的 View 。例如,我们输入指向 Home Controller 和 Index 方法的站点的主 url。此方法返回由 View 引擎渲染的 Index.aspx View 。
当 View 引擎呈现 Index.aspx 页面时,它会启动辅助方法,例如
<%this.RenderWidgets("sidebar1"); %>。
此方法真正负责呈现 aspx 页面中每个 div 的特定小部件。这样,如果您的用户更改小部件的布局,它们将正确显示在屏幕上。
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);
}
}
}
}
用户如何改变布局? Kona 有非常简洁的 javascript,它与 Ajax 一起使用,用户只需将小部件从一个面板拖放到另一个面板即可重新排列布局。
关于asp.net-mvc - .NET MVC : How to implement different page appearance per user?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2020744/