📜 ⬆️ ⬇️

Combat use of the C # compiler for WEB Forms

There are times when it is not possible to use to compile an assembly or a Visual Studio application. It sounds crazy, but it still happens sometimes. For example, it is necessary to compile a DLL from the configurator of a self-written SCADA system. This issue can be resolved using the CSharpCodeProvider class. But we will dig deeper and create WEB Forms application without using Visual Studio. Our main tool will be csc.exe. Compiler shipped with the .NET Framework. The experienced programmers will start a mean tear, remembering their first steps in .NET, and for beginners it will be useful to see the method of using the compiler and building the WEB Forms class of the application. So, let's begin…

Preparation and structure of our mini-project


To begin with, we will define the functionality that we want. We want a WEB Forms application containing one default.aspx form and which displays the message “It works” in a label on the page.

Project structure



default.aspx


Below is the markup code:
')
<%@ Page Language="C#" AutoEventWireup="false" Inherits="WForms_ByHands.MyPage" %> <!DOCTYPE> <html> <head runat="server"> <title>Ok</title> </head> <body> <form runat="server" id="myForm"> <div> <asp:Label ID="testLabel" runat="server" Text=""></asp:Label> </div> </form> </body> </html> 

We have a form (form) myForm, and a label (asp: Label) testLabel, in which we will display our message.
We will return to the “Inherits” attribute of the @ Page directive later.

default.aspx.cs


 using System; namespace WForms_ByHands { public partial class MyPage : System.Web.UI.Page { protected global::System.Web.UI.HtmlControls.HtmlForm myForm; protected global::System.Web.UI.WebControls.Label testLabel; public MyPage() { this.LoadComplete += Page_Load; } protected void Page_Load(object Sender, EventArgs e) { this.testLabel.Text = " !"; } } } 

We declare the page class of our application as WForms_ByHands.MyPage. This class must be specified in the “Inherits” attribute of our form. This class will be loaded when accessing our form. Since the “AutoEventWireup” attribute is set to “false”, we must explicitly specify event-procedure handlers in the constructor. For example, the event "LoadComplete" is selected - full loading of all page elements.

Compilation


To compile our application, we can write all the parameters at once on the command line, but use the response file. The response file can contain all compiler options and is the only parameter on the command line for csc.exe. Below is the response file code:

 /reference:System.Web.dll /*  ,   System.Web.UI.HtmlControls.HtmlForm  System.Web.UI.WebControls.Label */ /target:library /out:%root%\deploy\bin\WForms_Handly.dll %root%\source\default.aspx.cs 

Also in the% root% \ deploy directory you need to add web.config with the minimum required code:

 <?xml version="1.0" encoding="utf-8"?> <configuration></configuration> 

To compile the project, a bat file was created, the contents of which are given below:

 csc @deploy.rsp iisexpress /path:%root%\deploy /port:80 /clr:v4.0 /*     ,   IIS Express,      .   WEB-   IIS.*/ 

As a result of deploy.bat in the% root% \ deploy \ bin \ directory, we will find a new file - the assembly of the WForms_ByHands.dll application. Having requested the resource URL in the browser (in my case http: \\ localhost), we will see the proud text “It Works!”.

Source: https://habr.com/ru/post/251945/


All Articles