View 中的我的助手,旨在显示在应用程序中注册的用户的全名,以及通过第 3 方身份验证登录的用户名。
@using MyApp.Models;
@helper GetUserName()
{
if (User.Identity.AuthenticationType == "Application")
{
using (var db = new MyAppDbContext())
{
@db.Users.Find(User.Identity.GetUserId()).FullName
}
}
else
{
@User.Identity.GetUserName()
}
}
然后我使用这个助手:
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-form pull-right" }))
{
@Html.AntiForgeryToken()
<ul class="nav">
<li>
@Html.ActionLink("Hello " + GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
</li>
<li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>
}
}
我的用户名为Proba234,显示如下:
为什么会出现那些奇怪的字符(<$G+$>
),如何去掉它们?
最佳答案
这可能是与 Visual Studio Page Inspector 中帮助程序的使用相关的某种功能或错误,您可能不会在外部浏览器下看到这些标签。我在 VS 2013 中轻松复制了它。关于它的线程可以在 ASP.NET forum 上找到,例如
关于c# - <$G+$> 出现在 ASP.NET MVC5 Helper 中的奇怪字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966377/