用户在ASP.NET MVC模板中不存在?

时间:2011-09-04 12:46:15

标签: asp.net-mvc templates user-interface user-controls

您好,

我即将将视图的很大一部分移动到模板中。这样做可以在多个页面上显示内容。

问题是我在这个视图中对用户对象进行了检查,如下所示:

<% if(User.Identity != null && User.Identity.Name.Length > 0) {%>

当将它放在模板视图中时,我将得到以下异常:

Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0103: The name 'User' does not exist in the current context

请求建议。

1 个答案:

答案 0 :(得分:3)

尝试从上下文中获取它:

<% if (ViewContext.HttpContext.User.Identity != null && ViewContext.HttpContext.User.Identity.Name.Length > 0) { %>