Интернет-приложение по умолчанию, созданное Visual Studio, использует _LogOnPartial.cshtml, чтобы сделать именно это.
Значение имени пользователя устанавливается в действии LogOn HomeController
Код для _LogOnPartial.cshtml
@if(Request.IsAuthenticated) {
<text>Welcome <strong>@User.Identity.Name</strong>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@:[ @Html.ActionLink("Log On", "LogOn", "Account") ]
}
User.Identity является частью поставщика членства aspnet.
Код для _Layout.cshtml
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
<header>
<div id="title">
<h1>Test</h1>
</div>
<div id="logindisplay">
@Html.Partial("_LogOnPartial")
</div>
<nav>
<ul id="menu">
</ul>
</nav>
</header>
<section id="main">
@RenderBody()
</section>
<footer>
</footer>
</div>
</body>
</html>
Код для действия входа в 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<T> : WebViewPage<T>
{
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;
}
}
}
Вот код для просмотра
@{
ViewBag.Title = "PageTitle";
Layout = @ViewBag.LayoutModel.LayoutFile;
}
@using (Html.BeginForm())
{
<span class="error">@ViewBag.ErrorMessage</span>
<input type="hidden" name="Referrer" id="Referrer" value="@ViewBag.Referrer" />
html stuff here
}
Обратитесь к следующему вопросу для получения более подробной информации. Убедитесь, что вы изменили ваш web.config, как описано здесь: Как установить свойства ViewBag для всех видов без использования базового класса для контроллеров?