我正在尝试在ASP.NET MVC 5中创建用户控件。控件名称为LoginUserControl.ascx并保存在Views下的Shared文件夹中。我试图在_Layout.cshtml中调用用户控件,但是它给出了错误?请建议正确的方法。
.ASCX代码:
<%@ Control Language="C#"
AutoEventWireup="true"
CodeBehind="LoginUserWebControl.ascx.cs"
Inherits="TestApp.Views.Shared.LoginUserWebControl" %>
<%
if (Request.IsAuthenticated) {
%>
Welcome <b><%: Page.User.Identity.Name %></b>!
@Html.ActionLink("Log Off", "LogOff", "Account")
<%
}
else {
%>
@Html.ActionLink("Log On", "LogOn", "Account")
<%
}
%>
添加Layout.cshtml
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>@Html.ActionLink("Home", "Index", "Home")</li>
<li>@Html.ActionLink("About", "About", "Home")</li>
<li>@Html.ActionLink("Contact", "Contact", "Home")</li>
<li>@Html.Partial("LoginUserWebControl")</li>
</ul>
错误:
'〜/ Views / Shared / LoginUserWebControl.ascx'的视图必须来自 ViewPage,ViewPage,ViewUserControl或 ViewUserControl。 块引用
答案 0 :(得分:0)
您的LoginUserWebControl.ascx.cs必须从ViewUserControl派生,如下所示:
partial class LoginUserWebControl : System.Web.Mvc.ViewUserControl
{
...
}