在MVC3使用Page.User.Identity.Name [英] Using Page.User.Identity.Name in MVC3

查看:525
本文介绍了在MVC3使用Page.User.Identity.Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC2我已经使用 Page.User.Identity.Name 使用<%@控制语言=C#继承=系统.Web.Mvc.ViewUserControl%GT;

In MVC2 I have used Page.User.Identity.Name using the <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %>

我如何使用相同的MVC3?

How can I use the same in MVC3?

推荐答案

您总是可以这样做:

@Html.ViewContext.HttpContext.User.Identity.Name

但不要。

通常视图不应该尝试获取这些信息。它的存在可显示的任何信息是由控制器通过。它应该是强类型到由控制器操作通过一个模型类。

Normally a view shouldn't try to fetch such information. It is there to display whatever information is passed by the controller. It should be strongly typed to a model class which is passed by a controller action.

因此​​,在控制器动作呈现这样的观点:

So in the controller action rendering this view:

[Authorize]
public ActionResult Index()
{
    var model = new MyViewModel
    {
        Username = User.Identity.Name
    }
    return View(model);
}

现在在视图中随意使用这些信息:

Now inside the view feel free to use this information:

@Model.Username

这篇关于在MVC3使用Page.User.Identity.Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆