我在ASP.NET Web应用程序中创建了一个单独的文件夹和页面。当我构建解决方案时,我收到错误
The Namespace MyApp already contains a defintion for VDS
以下是VDS.Master.cs的内容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MayApp{
public partial class VDS : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
}
以下是VDS.Master.designer.cs的内容:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MyApp.VDS {
public partial class VDS {
/// <summary>
/// Head1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlHead Head1;
/// <summary>
/// head control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.ContentPlaceHolder head;
/// <summary>
/// form1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.HtmlControls.HtmlForm form1;
/// <summary>
/// ScriptManager1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.ScriptManager ScriptManager1;
/// <summary>
/// NavMenu control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.Menu NavMenu;
/// <summary>
/// smds1 control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.SiteMapDataSource smds1;
/// <summary>
/// MainContent control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
protected global::System.Web.UI.WebControls.ContentPlaceHolder MainContent;
/// <summary>
/// lblfoot control.
/// </summary>
/// <remarks>
/// Auto-generated field.
/// To modify move field declaration from designer file to code-behind file.
/// </remarks>
以下是VDS.Master的内容:
<%@ Master Language="C#" AutoEventWireup="True" CodeBehind="VDS.Master.cs" Inherits="MyApp.VDS.VDS" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Dealer Services</title>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
<link href="Styles/master.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<div class="header">
<h1>Welcome to Dealer Services </h1>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
</div>
<div class=" clear nav">
<asp:Menu runat="server" ID="NavMenu" BackColor="Silver" DataSourceID="smds1"
DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em"
ForeColor="White" Orientation="Horizontal" StaticSubMenuIndent="10px">
<DynamicHoverStyle BackColor="#284E98" ForeColor="White" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicMenuStyle BackColor="#B5C7DE" />
<DynamicSelectedStyle BackColor="#507CD1" />
<StaticHoverStyle BackColor="#284E98" ForeColor="White" />
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<StaticSelectedStyle BackColor="#507CD1" />
</asp:Menu>
<asp:SiteMapDataSource ID="smds1" runat="server" ShowStartingNode="False" />
</div>
<div class="login">
</div>
<div class="content">
<asp:ContentPlaceHolder id="MainContent" runat="server">
</asp:ContentPlaceHolder>
</div>
<div class="footer">
<asp:Label runat="server" ID="lblfoot">™ Veehco Inc. 2011</asp:Label>
</div>
</div>
</form>
</body>
</html>
我尝试删除VDS.Master.designer.cs文件,但每次构建时都会返回错误。我该如何纠正这个问题?
非常感谢!
答案 0 :(得分:8)
您是否有机会从网站将其转换为Web应用程序?我有时会看到转换引起的这个问题。
VDS.master文件的第一行可能如下所示:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="VDS.master.cs" Inherits="VDS" %>
至少在我的情况下,问题是它使用CodeFile
属性而不是CodeBehind
。如果您的项目确实是一个Web应用程序而且上面的行包含CodeFile
,那么您需要将其更改为CodeBehind
,因此它看起来像这样:
<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="VDS.master.cs" Inherits="VDS" %>
错误的原因是由于处理这两个属性的方式:
如果你的项目是一个Web应用程序,但它正在使用CodeFile属性,它最终会被你编译,然后在运行时编译,导致两个不同的程序集包含相同类的定义。然后一切都爆炸了。
答案 1 :(得分:4)
您是否有任何机会拥有与命名空间相同的文件?
例如,主文件的名称与命名空间和项目相同!
答案 2 :(得分:0)
在designer和.master文件中,您有一个与VDS类冲突的VDS命名空间。
更改.master中的继承:
Inherits="MyApp.VDS.VDS"
为:
Inherits="MyApp.VDS"
和设计师档案来自:
namespace MyApp.VDS {
为:
namespace MyApp {
答案 3 :(得分:0)
乔尔是正确的,我有同样的问题,并倾向于重命名和采取捷径。但是,花时间进行调查是值得重新建立对该工具的信心。
我在项目名称MyNameSpace中有一个Admin类。
我正在尝试创建MyNamespage.Admin。
我将Admin类更改为Administrator并且它可以正常工作。
答案 4 :(得分:0)
这意味着您已经有1个定义VDS,在您的命名空间中,您必须找到。 这通常是因为您在VS Studio中移动或编辑文件
在你的情况下,
namespace MyApp{
public partial class VDS : System.Web.UI.MasterPage
这意味着,您已经定义了MayApp.VDS
之后。在你的设计中
namespace MyApp.VDS {
public partial class VDS {
MyApp.VDS不是命名空间,VDS是一个类
所以你只需删除设计文件中的vds即可 现在将
namespace MyApp{
public partial class VDS {