Pergunta

I've worked on VS projects before where there is no .designer.cs files.

Now I started a new project on a different computer and I can't get rid of designer.cs files. It's really annoying me. Do I really need it, how can I remove it? There must be a setting somewhere.

Foi útil?

Solução

You're dealing with a web application rather than a website (clarification)

Yes, in the context of a web application, you do need them.

Outras dicas

YES! You can remove them......here is how.....

HOW TO DELETE DESIGNER.CS PAGES FROM YOUR WEB APPLICATION

After great torture and much testing seeking a way to avoid designer.cs pages in Visual Studio (v. 2015 and earlier), I finally found a work around for this. If anyone is stuck with designer.cs pages in a Web Application Project in Visual Studio this solution will allow you to erase all application compile errors quickly then delete the designer.cs pages completely from your project.

First understand the following:

Web Application Projects use designer.cs pages (partial classes) auto-generated by Visual Studio, and which are tied to the design tools built into Web Applications which the web app model sustains. I call it web sites for dummies. I could find no setting or way to turn the creation of partial classes and designer.cs pages off, as they are often unnecessary code tied more to the IDE than the functioning of the application. All partial classes get compiled into one class anyway. Web Apps also get pre-compiled or built ahead of time in general by design, and those dll's gets pushed into bin folders.

Web Site Projects do not use designer.cs files and are less tied to the IDE. They allow for a cleaner coding of class structures. They also use partial classes. But Web Sites Projects generally get compiled at runtime.

SOLUTION - How to Remove "...designer.cs" errors and files from a Web Application Project in Visual Studio.

Unless you want to convert your web project from an Application to a Web Site model in Visual Studio, this solution below allows you to run your project as is, yet like a Web Site, where you can move most or all the designer.cs partial class control references from that file into your main web page partial class file. It also removes all errors AND does not interfere with the Visual Studio recreating those designer files should other developers be sharing them and adding controls to pages and forget to use this solution on specific pages.

  1. In the front-end .aspx pages, in the @PAGE directive at the top, change the "CodeBehind" to "CodeFile". Make sure it still references the same .cs code or class file.

  2. Add the "CodeFileBaseClass" attribute to the same @Page directive in your web page and have it access the fully qualified path to the same .cs above with any namespaces in the path.

  3. Make sure you use the "Inherit" attribute with the same path as the "CodeFileBaseClass".

You should have the following for every web page in your Web Application with these attributes formatted as such:

<%@ Page Language="c#" CodeFile="index.aspx.cs" CodeFileBaseClass="YourNameSpace.index" 
 Inherits="YourNameSpace.index" %>

Now go into your designer.cs file for the page and copy any control references from the partial class in your designer file into the partial class of your main .cs file for the web page. If you have lost your designer.cs files, just add in any web controls as field references as fields that the compiler says are missing. After you do that DELETE the designer.cs file. You don't need them and have complete control over your web page controls using the main .cs file and its partial class.

Below is what I had in the designer.cs file before deleting it. Below it is the main index.aspx.cs file for my project after I added the designers.cs control reference as a field in its partial class. I then deleted the designer.cs file and its code completely from my project:

namespace YourNameSpace
{
public partial class WebForm1
{

    /// <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.Literal Message1;
}
}

In my index.aspx.cs file below you can see where I pasted the last "Message1" reference line from the designer above into its partial class at the top. You can see how I was then able to access the web page Literal control, Message1, in my Page_Load event and modify the text. That shows it was referenced correctly now, compiled, and worked, where before such references failed if the designer.cs file was missing or its partial class failed:

namespace YourNameSpace
{
public partial class index : System.Web.UI.Page
{
    protected global::System.Web.UI.WebControls.Literal Message1;

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Message1.Text = "hello world";
    }
}
}

The key to fixing this is the CodeFileBaseClass and CodeFile attributes in the @Page directive in the main web page. This prevents new member references and controls in the web page from being automatically generated by the Visual Studio IE and stuck as stubs in designer page partial classes (which to me is not helpful when you want full control over those fields in a single convenient area).

Note: If you later regenerate the designer.cs files again for your project by selecting your project in VS, then in the top menu choosing Project > "Convert to web Application" those designer.cs files will get recreated, BUT Visual Studio will ONLY place references to missing web controls in them that are NOT now added to your main .cs partial class page. In other words, any references I copied and added above to my index.aspx.cs partial class file would not be recreated in the new designer.cs file. So there is no conflict with the Web Application model you are using. When you see those designer files, you can now have developers safely copy them into your main code behind .cs files and safely delete the designers if you like.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top