Call c# code within aspx page

Hi all:

i am new to .net development, just a question regarding c# and

for example:

i have following c# code

class Hello
public Hello()

public int add(int i, int j)
    return i+j;


so my question is:

how can i call use that Hello class in an aspx page?


ok, now i knew , i need to compile that c# class into DLL, and then in aspx , import that dll.

but can any one tell me how to use visual express 2005 c# to produce a global(shared) assembly.

i tried use [assembly: AssemblyKeyFile] to include strong name key file, but visual c# gave me a warning says “i’d better use command line switch /keyfile to add strong name key file.”. i just can not find out how to modify c# compiler switch within visual 2005 c# IDE


Just drop the .cs file in the App_Code directory. If you make it public it will be readily available to any aspx page or ascx control. No need to compile assemblies.

you only have to compile assemblies if you’re working with winforms in Visual C#, it’s different in ASP.NET

That’s one of the reasons they recommend Visual Web Developer if you’re working with webpages. Visual C# and Visual Basic really only work with Windows applications.

Actually, you code either C# or VB.NET when coding ASP.NET (other languages exists but these are by far the common ones).

C# and VB.NET is very much the code in ASP.NET pages, components etc.

Also, it’s really the webserver which does the compilation on-the-fly. You have the same experience as with scripts: You just upload the files and they are ready to run without explicit compilation. But the server does compile every single aspx page, every ascx control, all of the .vb or .cs (C#) files. Only it is completely transparent (apart from the occasional delay and the loss of sessions).

narf thank you for clarifying, honeymonster.

What I meant to say was that the editing programs Visual C# and Visual Basic are for building winforms for Windows applications.

The program that is therefore recommended for building webforms for webpages is Visual Web Developer(VWD). However, C# and VB are the programming languages you use to program ASP.NET webpages in VWD. I didn’t mean to be confusing, apologies if I wasn’t clear.

Ok, that explains it. You were referring to the Visual C# Express Edition and the Visual Basic .NET Express Editions?

I must confess that I’ve never really used them, so I am not thinking about C# as an editing program. But I can see why you would, if you use the express editions (I’m just spoiled by the Team Suite where there’s no special edition per language).

ok, i have put following into my aspx page.

<%@ Page language=“C#” Inherits=“System.Web.UI.Page” CodeFile=“~/App_Code/Hello.cs” %>

now, VWD gave me an error message:

" Error 1 The file ‘/WebSite1/App_Code/Hello.cs’ is in the special directory ‘App_Code’, which is not allowed. ".

and by the way, my c# code must not inherits from “System.Web.UI.Page”, because it will be used in non - web environment.


Uh no, the codefile for a page does not go in the App_Code directory. Your original question was how to use a hello class which was obviously not a page class.

The codefile should sit alongside the hellopage.aspx file (not in the App_Code directory). Although you are free to give the file any name you like, you should just call it hellopage.aspx.cs. The reference from the aspx file will ensure that it is compiled with the aspx file (they really make up a class in union, each is a partial class).

You should read up on how to design the codebehind/beside classes. Your hello class is not a page class, but it can be used by the code in a page class.

Assuming that your Hello class was to become the “model”:


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HelloPage.aspx.cs" Inherits="HelloPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
    <title>Hello Page</title>
    <form id="form1" runat="server">
        i: <asp:TextBox ID="I" runat="server" /><br />
        j: <asp:TextBox ID="J" runat="server" /><br />
        <asp:Button ID="Add" runat="server" Text="Add" OnClick="Add_Click" /><br />
        Result: <asp:Label ID="Result" runat="server" Text="" />

HelloPage.aspx.cs (same directory as above):

using System;
using System.Web.UI;

public partial class HelloPage : Page
    protected void Add_Click(object sender, EventArgs e)
        Hello h = new Hello();
        Result.Text = h.Add(int.Parse(I.Text), int.Parse(J.Text)).ToString();

Assuming your Hello.cs file sits in the App_Code directory:

public class Hello
    public int Add(int i, int j)
        return i + j;

How Suite!:wink: Yes, I’m afraid I’m part of the independent developer on a budget group. I’m using the Express editions til I get full time employment.

And forgive the VBer in the group. If I’m reading this correctly, this looks like the C# syntax for creating the hello class with it’s Add method? I have played around with custom classes a bit in VB but not C# at all.

You don’t need to make any reference to Hello.cs in your .aspx pages, correct? Anything in the App_Code folder is understood to be part of the application and it’s automatically available? I’m thinking as opposed to things like database connection strings that you still have to reference in the web.config or things like that. You don’t need a reference to Hello.cs, you just call the call = new Hello() and it’s a good boy and comes when your call it?

correct and yes; with one caveat.

Explanation: Anything in App_Code is compiled into an assembly. Like any assembly in the /bin directory, any page can readily use its types. However, the classes may be defined inside namespaces. In that case any reference must use the qualified name (prefixing with the namespace) or import the namespace.

The hello class above (implemented in hello.cs) declared no namespace. Hence, it lives in the “global” namespace. That is ok if it is in a web site with few classes in App_Code, as it they are not going to be used in any other project. But once the number of classes grow you may want to organize the classes into namespaces.

you’re awesome. Thanks for being patient with my pestering:D
I appreciate it.

Even better, once you have many classes I’d go so far as to say you shouldn’t use App_Code at all. In fact, I don’t use it at all. I think it’s much more organized to separate them into separate class libraries and set references from the web project.

But you can’t do that in vwd express, so App_Code is all you have. In that case do separate them into namespaces then (and folders).

However, to chroniclemaster1, vc# isn’t only for building windows applications. As mentioned you can create class libraries for use in web projects. Admittedly I dont’ know if you can do that between vc# and vwd express, because I’ve had Visual Studio standard for quite a while now. In visual Studio you can easily create class libraries, then have a web project that references those projects directly and so are compiled in with the web site.

I’ve seen the class libraries options, but they gloss over it in the courses I’ve taken and I haven’t been able study that as well as I’d like. That is one of the options that you lose in the Express edition. Precisely I’m sure because it’s meant to be frustrating and make you buy VS. Visual C# Express has an even more restricted selection, I think the express edition has only a couple options besides the basic winform, compared to what, 12 or 15 in VS? Oh well, even in VB I never really got defining namespaces down properly. That’s probably on the back burner for now. I’m programming my contact form in for my website in VWD using VB, and once I’m finished I’m going back to my beta website for testing and redoing it in C#. There’s always a new project that I want to do, but most of the jobs I’ve been looking for are much more interested in C#. It’s time for me to switch and be a newbie again. I just hate the dunce cap!:wink:

Visual Studio Standard really is a great investment as it really does give you a lot more options that you don’t have in express. I was lucky and got it for free when Microsoft ran their promotion last year (view their webcasts and they gave Visual Studio 2005 Standard for free), but I think it’s well worth paying for.

Regarding the language, haven’t you used any language such as java, C++, etc? VB is probably more unlike any other language and so C# is a lot more intuitive if you have used C-like languages. The C# syntax was really no difficulty for me at all when I started learning .NET because I had used C++ extensively in the past and had a little experience with Java. In fact with the MSDN Library and Intellisense in Visual Studio, I could pretty much get my way through about anything. The only trouble was figuring out the pages of ASP.NET, and then more than anything trying to figure out the best practices in ASP.NET, as it was quickly evident that the same practices I was used to in PHP weren’t very common in .NET (such as MVC and so forth). But after figuring out my preferred way of handling business objects and data access, it was fairly simple.

Don’t mean to hijack this thread by the way.

app_code is ok for small size project, all you need to do is put all c# classes into app_code folder, and those c# classes will be compiled into a temporary assembly . but speed is unacceptable.

app_code is a special folder, you can not just create a class and copy into it, you need to do it in VWD. i have compare plain c# class and VWD created c# class, VWD created class added following references:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

also visual c# can also be use to create web site, all you need is create c# class library and reference those class library in your web project.

i think using visual c# to create class library then reference the class library is better. leave VWD as a tool for manipulate ASP net controlers.


ctx2002, I agree with most everything you say, except…that you have to use VWD to put a class into App_Code. The only reason it puts those using statements in its class files is because those are some of the most common. I have made my own item template for creating classes that only references System (and adds comments about the author etc), and it works just fine. I’d rather put in additional using statements as they are required than have unnecessary ones, though it doesn’t make much difference besides cluttering the environment with unnecessary types.

I’m sure it makes almost no difference to the server, but just the idea that there would be a using statement which is unnecessary rubs me the wrong way. I’m sure it’s fine, but in my head it represents a webpage that hasn’t been “cleaned, documented, and tuned” and that bugs me.

But that’s just me, and I’m loopy.:slight_smile:

Unless you count 6 months of playing with Borland C++ about oh… wow, I’m not even gonna tell you how many years ago that was! No, I really don’t have much experience with C languages. I work with Javascript at a sort of intermediate level, but other than that VB.NET is the only other language I really use. So, yeah, it’s going to be a stretch. And while I’m familiar with OOP, I’m sure you can appreciate the gap between there and planning and writing your entire program on the OOP model. Like I said, I keep putting it off because I’ll be a newbie again, and will be back here with LOTS of stupid questions on C#. Aren’t you excited already!:smiley:

Hey I feel the same way about the using statements. It really doesn’t matter because all those libraries are compiled into the assembly anyway, but still I am a minimalist and don’t like unneeded namespaces to be imported.

Regarding C languages, heh I’ve messed with Borland C++ as well. :smiley: C++ is really a language to mess with.

I’m sure you’ll catch on to C# rather easily.