Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make ASP.NET application using Notepad

I have made an ASP.NET application using Notepad++. For this exercise I do not want to use Visual Studio, or any other tool. I want to understand the process.

I have created my website, and it is up and running fine, and all working well.

Now I want to add some C# code behind the pages, both for the master page and for individual pages.

So far, I have a file called Home.aspx, and I want to add a C# file to this.

I have created a file called Home.aspx.cs. Below is the full content of the file:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Write("LOAD");
    Response.End();
}

But when the page loads, this file is not loading. Obviously I am missing something, but I am not sure what. Possibly a reference in my web.config or some other folder, or language reference to tell the page this is C#, or something to tell Page_Load to actually run?

Also, I want to do the same thing for my master page, which is currently called masterPage.master.

So would I make a file called masterPage.master.cs, or is it a totally different way, or can this even be done?

All references to this problem explain how to do this in Visual Studio, which I do not want to use.

like image 772
user4420358 Avatar asked Mar 15 '23 22:03

user4420358


1 Answers

You can in fact create an ASP.NET WebForms page without compiling .cs files explicitly.

Home.aspx

<%@ Page Src="Home.aspx.cs" Inherits="HomePage" AutoEventWireup="True" %>

Notice that the @ Page directive uses the Src attribute instead of the usual CodeBehind attribute.

(Instead of Src, you can alternatively use the CodeFile attribute and mark the code-behind class below partial.)

Home.aspx.cs

using System;
using System.Web.UI;

public class HomePage : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("LOAD");
        Response.End();
    }
}

masterPage.master

<%@ Master Src="masterPage.master.cs" Inherits="MasterPage" AutoEventWireup="True" %>

Same thing, except that you use the @ Master directive instead of @ Page.

(Again, instead of Src, you can alternatively use the CodeFile attribute and mark the code-behind class below partial.)

masterPage.master.cs

public class MasterPage : System.Web.UI.MasterPage
{
}

(I named the code-behind class MasterPage to match your file name, but to avoid confusion with the built-in ASP.NET MasterPage base class, you may want to choose a different name.)

like image 184
Michael Liu Avatar answered Mar 23 '23 02:03

Michael Liu