How to delete ASP.NET Designer.cs files?

I worked on VS projects before where there are no .designer.cs files.

Now I started a new project on another computer, and I can not get rid of the designer.cs files. It annoys me a lot. I really need it, how can I remove it? There should be a setting somewhere.

+2
visual-studio partial-methods
source share
2 answers

You are dealing with a web application, not a website ( clarification )

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

+2
source share

YES! You can delete them ... here's how .....

HOW TO REMOVE DESIGNER.CS PAGES FROM YOUR WEB APPLICATION

After much torture and many trials, trying to avoid the possibility of creating designer.cs pages in Visual Studio (version 2015 and earlier versions), I finally found a job for this. If someone is stuck with designer.cs pages in a web application project in Visual Studio, this solution will allow you to quickly remove all application compilation errors and then completely remove designer.cs pages from your project.

First understand the following:

Web application projects, use the designer.cs pages (partial classes) automatically created by Visual Studio that are tied to the design tools built into the web applications supported by the web application model. I call it dummy websites. I could not find the settings or a way to disable the creation of partial classes and designer.cs pages, since they are often unnecessary code related more to the IDE than to the functioning of the application. All partial classes are still compiled into one class. Web applications are also pre-compiled or created in advance, in general, by design, and these dlls fall into the bin folders.

Website projects do not use designer.cs files and are less attached to the IDE. They allow cleaner coding of class structures. They also use partial classes. But website designs are usually compiled at runtime.

DECISION. How to remove "... designer.cs" errors and files from a web application project in Visual Studio.

If you do not want to convert your web project from an application to a website model in Visual Studio, this solution below allows you to run the project as is, but as a website on which you can move most or all .cs developers partial control the link class from this file to the partial class file of your main web page. It also removes all errors and does not prevent Visual Studio from re-creating these designer files if other developers have to share them and add controls to the pages and forget to use this solution on certain pages.

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

  • Add the "CodeFileBaseClass" attribute to the same @Page directive on your web page and access the full path to the same .cs above with any namespaces in the path.

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

For each web page in your web application, there should be the following: these attributes are formatted as follows:

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

Now go to your designer.cs file for the page and copy any control links from the partial class in the designer file to the partial class of your main .cs file for the web page. If you have lost designer.cs files, simply add fields to the website control as fields that, according to the compiler, are missing. After that, make a DELETE file designer.cs. They don’t need you and have full control over the controls of the web page using the main .cs file and its partial class.

Below was 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 references.cs as a field in its partial class. Then I 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" link from the constructor above into my incomplete class at the top. You can see how I was able to access the Literal control web page, Message1, in my Page_Load event and change the text. This shows that now it was correctly edited, compiled and worked, where before such links failed if the designer.cs file was missing or its incomplete 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 on the main web page. This prevents the automatic creation of new link elements and controls on a web page in Visual Studio IE and stuck as stubs in partial classes of designer pages (which is not useful for me when you want to completely control these fields in one convenient area).

Note. If you later regenerate designer.cs files again for your project by selecting your project in VS, then select "Project"> "Convert to Web Application" from the top menu, these designer.cs files will be restored, BUT Visual Studio will ONLY post links to missing web controls in them that are NOT added to your main .cs partial class page. In other words, any links that I copied and added above to my partial.aspx.cs partial class file will not be recreated in the new designer.cs file. Therefore, the conflict with the web application model is not used. When you see these designer files, now you can safely copy them to your main code behind the .cs files and safely delete the designers if you want.

0
source share

All Articles