名称空间*已包含*的定义

Sid*_*idC 4 c# asp.net

我在ASP.NET Web应用程序中创建了一个单独的文件夹和页面.当我构建解决方案时,我收到错误

The Namespace MyApp already contains a defintion for VDS
Run Code Online (Sandbox Code Playgroud)

这是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)
    {

    }
}
}
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

这是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">&trade; Veehco Inc. 2011</asp:Label>
</div>


</div>  


</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

我已经尝试删除VDS.Master.designer.cs文件,但每次构建时都会返回错误.我该如何纠正这个问题?

非常感谢!

Joe*_*ham 8

您是否有机会从网站将其转换为Web应用程序?我有时会看到转换引起的这个问题.

您的VDS.master文件的第一行可能如下所示:

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="VDS.master.cs" Inherits="VDS" %>
Run Code Online (Sandbox Code Playgroud)

至少在我的情况下,问题是它使用的是CodeFile属性而不是CodeBehind.如果你的项目确实是一个Web应用程序并且上面的行包含CodeFile,那么你需要将它改为,CodeBehind所以它看起来像这样:

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="VDS.master.cs" Inherits="VDS" %>
Run Code Online (Sandbox Code Playgroud)

出错的原因是这两个属性的处理方式:

  • CodeBehind:需要在部署之前编译,编译的程序集放在您网站的bin文件夹中.
  • CodeFile:您部署源代码并根据需要进行编译.已编译的程序集放在Temporary ASP.NET文件夹中.

如果您的项目是一个Web应用程序,但它正在使用CodeFile属性,它最终由您编译,然后在运行时编译,导致两个不同的程序集包含相同类的定义.然后一切都爆炸了.


小智 5

您是否有一个与命名空间命名相同的文件?

例如,主文件的命名与命名空间和项目相同!