我有Razor引擎的MVC 3 C#项目.有什么方法,我想,将动态数据写入_Layout.cshtml的最佳做法是什么?
例如,也许我想在我的网站的右上角显示用户名,并且该名称来自会话,数据库或其他任何基于用户登录的内容.
更新:我也在寻找将某些数据渲染到布局元素的良好实践.例如,如果我需要根据登录用户的凭据呈现特定的CSS文件.
(对于上面的例子,我想过使用Url Helpers.)
visual studio创建的默认Internet应用程序使用_LogOnPartial.cshtml来完成此操作.
用户名值在HomeController的LogOn操作中设置
_LogOnPartial.cshtml的代码
@if(Request.IsAuthenticated) {Welcome @User.Identity.Name! [ @Html.ActionLink("Log Off", "LogOff", "Account") ] } else { @:[ @Html.ActionLink("Log On", "LogOn", "Account") ] }
User.Identity是aspnet成员资格提供程序的一部分.
_Layout.cshtml的代码
@ViewBag.Title Test
@Html.Partial("_LogOnPartial")@RenderBody()
AccountController登录操作的代码
[HttpPost] public ActionResult LogOn(LogOnModel model, string returnUrl) { if (ModelState.IsValid) { if (Membership.ValidateUser(model.UserName, model.Password)) { FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/") && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\")) { return Redirect(returnUrl); } else { return RedirectToAction("Index", "Home"); } } else { ModelState.AddModelError("", "The user name or password provided is incorrect."); } } // If we got this far, something failed, redisplay form return View(model); }
ApplicationViewPage类的代码
public abstract class ApplicationViewPage: WebViewPage { protected override void InitializePage() { SetViewBagDefaultProperties(); base.InitializePage(); } private void SetViewBagDefaultProperties() { ViewBag.LayoutModel = new LayoutModel(Request.ServerVariables["SERVER_NAME"]); } }
上面的代码允许我有一个ViewBag.LayoutModel,它在每个页面中都包含我的LayoutModel类的实例.
这是我的LayoutModel类的代码
public class LayoutModel { public string LayoutFile { get; set; } public string IpsTop { get; set; } public string IpsBottom { get; set; } public string ProfileTop { get; set; } public string ProfileBottom { get; set; } public LayoutModel(string hostname) { switch (hostname.ToLower()) { default: LayoutFile = "~/Views/Shared/_BnlLayout.cshtml"; IpsBottom = "~/Template/_BnlIpsBottom.cshtml"; IpsTop = "~/Template/_BnlTop.cshtml"; ProfileTop = "~/Template/_BnlProfileTop.cshtml"; break; case "something.com": LayoutFile = "~/Views/Shared/_Layout.cshtml"; IpsBottom = "~/Template/_somethingBottom.cshtml"; IpsTop = "~/Template/_somethingTop.cshtml"; ProfileTop = "~/Template/_somethingProfileTop.cshtml"; break; } } }
这是View的代码
@{
ViewBag.Title = "PageTitle";
Layout = @ViewBag.LayoutModel.LayoutFile;
}
@using (Html.BeginForm())
{
@ViewBag.ErrorMessage
html stuff here
}
有关更多详细信息,请参阅以下问题.确保修改web.config,如下所述:如何在不使用控制器基类的情况下为所有视图设置ViewBag属性?