views:

41

answers:

2

Hey everyone, I am trying to (temporarily!) do some ASP.NET and C# compiling on my iMac.

Haha, it's so funny trying to do this on an iMac, it feels so.. wrong!

Anyway, I've found that Mono is the best way to do this, in my opinion.

I have a simple ASP.NET page with the following code:

1  <%@ Page Language="C#" %>    
2  <html>    
3  <head>    
4  <title>Planting Trees</title>    
5  <script runat="server">    
6    protected void Page_Load(Object Source, EventArgs E)    
7    {    
8      string msg = "Let's plant some trees!<br/>";    
9    
10     // Create a new Tree    
11     Tree tree1 = new Tree();    
12    
13     msg += "I've created a tree with a height of " +    
14       tree1.height + " metre(s).<br/>";    
15    
16     tree1.Grow();    
17    
18     msg += "After a bit of growth, it's now up to " +    
19       tree1.height + " metre(s) tall.<br/>";    
20    
21     Tree tree2 = new Tree();    
22     Tree tree3 = new Tree();    
23     tree2.Grow();    
24     tree3.Grow();    
25     tree2.Grow();    
26     tree3.Grow();    
27     tree2.Grow();    
28     msg += "Here are the final heights:<br/>";    
29     msg += " tree1: " + tree1.height + "m<br/>";    
30     msg += " tree2: " + tree2.height + "m<br/>";    
31     msg += " tree3: " + tree3.height + "m<br/>";    
32    
33     Output.Text = msg;    
34   }    
35 </script>    
36 </head>    
37 <body>    
38 <p><asp:label runat="server" id="Output" /></p>    
39 </body>    
40 </html>

Now, I have this simple C# class to go along with it:

// Tree.cs, A simple C# class

public class Tree {

  public int height = 0;

  public void Grow() {
    height += 1;
  }
}

Now, the ASP.NET page is located in my home directory on my iMac and the .cs C# class file is located under bin in the home directory.

Now, I'm not 100% sure, but do I need to compile the .cs into a .dll for this to work?

Any suggestions are greatly appreciated!

+1  A: 

How about mod_mono?

Mark Johnson
Hmm, doing some further reading, Mono states "The easiest way to start XSP is to run it from within the root directory of your application. It will serve requests on port 8080. Place additional assemblies in the bin directory." This is what I've been doing, except perhaps, like I stated above, I just need a .dll instead of a .cs file in the `bin` folder...
BOSS
+1  A: 

Mark Johnson's answer is correct - you'll need to setup mod_mono so you actually have an application server to serve up pages.

That said, you're right that you need to compile your .CS into a .NET assembly (DLL).

I can only speak from the Windows world, but I assume the mono world is much the same. You can't just compile your class and have it run - an ASP.NET web application expects a few things (like global.asax and web.config) so it knows how your pages should be served up.

I'm not sure what IDE you're using, but your best bet might be to download a simple Mono sample ASP.NET application and try to stuff in your own code. All the configuration and lifecycle features can be daunting at first and quite honestly you should forget they even exist while you're still just playing around with pages and codebehind.

Alternately you could try going through this CodeProject: http://www.codeproject.com/KB/cross-platform/introtomono2.aspx

It's quite out of date but the principles remain the same.

Stefan Mohr
Thank you very much! I was actually able to eliminate the `.dll` problem and just reference the class `.cs` file with the `src=""` attribute in the `@Page` directive of my .aspx file and the "server" handles it all magically! No need to create a .dll or deal with any other pages. For anyone who has this problem this was an easy solution and I found it with the above link! Awesome! :D
BOSS