ASP.net C# making custom classes

So how do you create a custom class file in asp without using visual studio or similare to compile your .dll?
Well, all you need to do is to make a c# class like below. And then compile it to a dll file. If you are using IIS, place the dll in a bin folder in your project folder.

if you use the root folder it would be:

C:\inetpub\wwwroot\bin

Compiling the dll is easy, but you need the following:

1. Download Microsoft.Net framework SDK v2.0
2. Locate the installation shortcuts in start menu, and open the SDK command prompt. Browse to the C:\inetpub\wwwroot\ and type in csc /target:library SqRtCustom.cs
3. move the SqRtCustom.cs in the bin folder
4. launch the aspx from ISS using adress localhost/index.aspx in your fav browser

The SqRtCustom.cs class file:


using System;
public class SqRtCustom {
  public SqRtCustom(double nr) {
     number = nr;
  }

  private bool isPos(){
    if(number >= 0){
       return true;
    } else {
    return false;
    }
}

public double sqRoot() {
  if(this.isPos()){
    return Math.Sqrt(this.number);
  } else {
       throw new Exception("Cannot take the square root of a negative nr!");
  }
  }
private double number;
}

The index.aspx file:


<html>
<head>
<title>Let's do some math</title>
<script runat = "server">
  protected void Page_Load(object Source, EventArgs E){
    SqRtCustom[] test = new SqRtCustom[3];
    test[0] = new SqRtCustom(-16);
    test[1] = new SqRtCustom(0);
    test[2] = new SqRtCustom(25);
    Response.Write("<h1>Square root of a nr:</h1><hr />");
    Response.Write("<ul>");
    for(int i = 0; i < test.Length; i++){
        try{
            Response.Write("<li>" + test[i].sqRoot() + "</li>");
        } catch(Exception error){
            Response.Write("<li>" + error.Message + "</li>");
        }
     }
Response.Write("</ul>");
}
</script>
</head>
<body>
</body>
</html>

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s