Combat application of the C # compiler for WEB Forms

There are times when it is not possible to use Visual Studio to compile an assembly or application. It sounds wild, but it does happen sometimes. For example, compilation of a DLL from the configurator of a self-written SCADA system is required. This issue can be solved using the CSharpCodeProvider class. But we dig deeper and create a WEB Forms application without using Visual Studio. Our main tool will be csc.exe. Compiler shipped with the .NET Framework. Inexperienced programmers will take a stingy tear, recalling their first steps in .NET, and it will be useful for beginners to see the method of applying the compiler and building the WEB Forms class of the application. So, let's begin…

The preparations and structure of our mini-project

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

Project structure

  • % root% \
    • deploy \
      • bin \
      • default.aspx
      • web.config
    • source \
      • default.aspx.cs
      • deploy.rsp
      • deploy.bat


Below is the markup code:

<%@ Page Language="C#" AutoEventWireup="false" Inherits="WForms_ByHands.MyPage" %> 


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


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 false, we must explicitly specify event handler procedures in the constructor. As an example, the event “LoadComplete” was selected - full loading of all page elements.


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

/reference:System.Web.dll /*ссылка на сборку, содержащую классы System.Web.UI.HtmlControls.HtmlForm и System.Web.UI.WebControls.Label */

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

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 executing deploy.bat in the% root% \ deploy \ bin \ directory, we find a new file - the assembly WForms_ByHands.dll of the application. Having requested the resource URL in the browser (in my case http: \\ localhost), we will see the proud text “It works!”.

Also popular now: