(aka “But it worked on Windows!?!”)
OK, so you’ve built a brilliant ASP.Net website, and now you want to show it to the world. You upload the pages to your webspace, only to discover it doesn’t work…
Well, there are very good reasons why, and it’s pretty simple to get it work, once you understand what’s going on. In this tutorial, we are going to understand how ASP.Net works here at x10Hosting, and how to deploy your projects successfully.
Background
How do you write and deploy an ASP.Net page? Well, for most of us, it’s as follows (see if you can spot the theme!):
So, not to hard to spot; Microsoft all the way so far. Now, let’s look at the deployment:
See anything missing? x10Hosting (like the majority of web Hosts) do not install Microsoft technologies. As there is competitive open-source (free) software on the market, there really is little need to pay Micro$oft (especially if you’re giving away some of your services for free, as our Host does…).
So can you use ASP.Net (a Microsoft technology) here at x10Hosting? The answer is a big fat Yes; absolutely. x10Hosting (unlike the majority of web hosts) have installed the Mono Project on their servers, which allows it to build ASP.Net pages.
In very simple terms, the Mono Project is an open-source copy of the .Net Framework. The Namespaces and Classes are the same in both frameworks, which should mean you do not have to change your code; what works on Windows/.Net, will also largely work on Linux/Mono.
Now, this isn’t always true for a few good reasons. As an open-source project, Mono lags behind in development compared to the might of the Microsoft team building the .Net Framework. VB.Net support is very poor; according to its documentation, only version VB8 (VB2005) is allowed currently. C# users will be happy to know they can utilise most of the latest (C# version 3) language enhancements, including LINQ.
As an ASP.Net developer, it will be important for you to look at the documentation, and familiarise yourself with the key (and subtle) differences between the frameworks. If you really get to know the Mono Project, you will discover it has some real gems of it’s own to offer.
Let’s get Coding…
When creating an ASP.Net page, you will create two files; pageName.aspx containing the xhtml, and pageName.aspx.vb or pageName.aspx.cs containing the code.
An XML document called web.config is also created. This contains configuration and settings for your site or a specific project. For now, we’re going to ignore this file. It is important, so we’ll come back to it later.
I’ll use a version of the world famous and painfully boring “Hello World!” program as an example of how easy it is to deploy an ASP.Net project, using C# or VB.Net. However, we’ll put a Button on ours, to make it exciting!
Using Visual Web Developer 2008 Express, create a new website. Ignore the Default.aspx, and add a new Web Form called HelloWorld.aspx. Now, place a Label and a Button on to the page.
Here’s the xHTML (HelloWorld.aspx):
HTML Code:
<!-- this line is for C# projects -->
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="HelloWorld.aspx.cs" Inherits="HelloWorld" %>
Or
HTML Code:
<!-- this line is for VB.Net projects -->
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="HelloWorld.aspx.vb" Inherits="HelloWorld" %>
And this is the rest of xhtml for both languages:
HTML Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Hello World!</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Erm, Hello World!"></asp:Label>
<br /> <br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Press Me" />
</div>
</form>
</body>
</html>
That’s the html sorted. Now on to the code-behind file.
C# HelloWorld.aspx.cs:
Code:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Asp.Net post test worked.";
}
}
Or
VB.Net HelloWorld.aspx.vb:
Code:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Label1.Text = "ASP.Net post test Worked"
End Sub
End Class
VB.Net users; you may have noticed that I am not attaching an Event Handler as we usually would, like this:
Code:
'incorrect code
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles Button1.Click
End Sub
This will produce an error when you deploy the page, as VB.Net support in Mono is very out of date. To solve this issue, we need to call the Methods by their name instead of attaching a Handles clause. As you can see, the Method is raised directly in the xhtml:
HTML Code:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
Run the project on your computer, to see it should work just fine. OK, now we’re almost ready to upload the files to our server. This is the really important bit – follow these steps:
- Do not upload the web.config file
- If you have a previous web.config file in your webspace; remove it (or them), as this may break the application
- Remove any ASP.Net pages from your site that may be broken
- Open your .htaccess file; if you have any reference to ASP.Net files in there, remove it
- Now, you may upload the two HelloWorld files you’ve just created to the root of your webspace (/public_html).
If you have performed the above steps correctly, when you navigate to your web domain /HelloWorld.aspx, you will see your page in all its glory – go on, press the Button; you know you want too…
Can you believe it was that simple? Me either! It seems that ASP.Net is as easy to utilise on x10Hosting servers as php or Python.
A trouble-free web.config
You will need a web.config file, to store database connection strings or to display specific error pages, for example. So, let’s create a new one. You may ask why create one when our project has created one already? Well, as I've shown, you don’t need all that stuff it creates, and some of it will cause trouble…
Here’s our new global web.config xml file:
Code:
<?xml version="1.0" ?>
<configuration>
<appSettings />
<connectionStrings />
<system.web>
<customErrors mode="Off" />
</system.web>
</configuration>
Now we’re done; Upload the file to the root of your webspace (/public_html).
Currently, our web.config only performs one task - if your ASP.Net pages have any errors on them, the specific error information will now be displayed for you, which will make your debugging so much easier. You can discover what else you can do with your web.config in the Config section of the Mono documentation.
As you placed this web.config in your root directory, it will apply to all ASP.Net pages in your webspace. You may add different web.config files to subdirectories only if you really need to manage specific pages contained in that subdirectory.
--------------------------------
Thank you for reading this tutorial for beginners; if you are an aspiring ASP.Net developer, I hope you will now find the process much easier. If you didn’t bother reading any of this, I basically took over 1,000 words to say “delete web.config”!
If anyone has any comments, questions or corrections, I would gratefully welcome them.