Are you a developer looking to get your .NET website hosted or interested in Azure: Microsoft’s cloud platform? This blog post is a step-by-step guide on how to take your website and database from your computer to the internet for everyone to see all within Microsoft’s ecosystem. Azure provides a handful of different services, but we will be going over how to host a SQL Server and a .NET website on Azure. If you don’t have a website we will get you started.
For the purposes of this example, you will need the following on your computer:
- A .NET website in Visual Studio
- SQL Server database
Don’t have any of these?
Microsoft has made this technology stack and its supporting software available for free with tutorials to help you get a project off the ground. Microsoft has put out an example web project, Contoso University. You can download it, implement it from scratch, or create your own website using the guide as a reference. By the end of it, you will have a .NET site and SQL Server database running on your local computer.
Getting into Azure
Microsoft allows anyone to sign up for a free one month trial. All you need is a Microsoft account, your mobile phone number, and a credit card. Even though they require a credit card number, they will not charge you anything so there is no need to take further action to prevent any future charges. They just want a credit card number to make any potential purchases easier. Now we will make an Azure account and get to the Azure Portal.
- Sign up for a free trial. Create a Microsoft account if you don’t already have one.
- Fill out the forms to get your way to your Azure Portal.
Deploy your Site
Think of your site like a hotel. If you want people to visit it, you need to find some land to build it on. Azure’s web hosting will act like that plot of land on which you will lay the foundation of your website. For now, your website url will end in “.azurewebsites.net” but can be changed later (I’ll show you how towards the end of this guide). The key piece here is the publishing profile. Using our hotel analogy, you can think of the publishing profile as the address to your hotel. You need to give those directions to your project so that it knows where to build the site. In these steps we will be creating space for the site in Azure and building our project to that location.
- In the Azure Portal, click “Websites” on the left pane then click the + button on the bottom left to create a new one.
- Click “Quick Create” and specify the URL of your site (all Azure websites end in “.azurewebsites.net”). Under Web Hosting Plan you can just select the default. Click Create Website and your new site will appear in your websites list and is available to browse to.
- Next we need the publishing profile. Locate the “Publish your app” header and click “Download the publish profile”. This file has the extension “.PublishSettings” which is a file type that Visual Studio uses to deploy a site.
- Import your publishing profile into your Visual Studio project.
- In Visual Studio, right-click on your web project and click “Publish”. You will be prompted to select a publish profile.
- Click “Import” which will open up a new window.
- Make sure “Import from a publish profile” is checked and press “Browse” to navigate to the “.PublishSettings” file you downloaded.
- Click publish and your deployed site will open up a new tab in your browser.
All you need to do to push updates to your site is right-clicking on the web project and clicking publish. This opens up the publish settings window with a Publish button. Click the button to publish your local website to your Azure website. A new browser window with your Azure website will open upon completion. If you’ve made CSS changes, you might need to do a hard refresh to load your CSS changes. This procedure was describing how to deploy to Azure from Visual Studio. If you wish to see the full range of ways to deploy to Azure, go to http://azure.microsoft.com/en-us/documentation/articles/web-sites-deploy/
Deploy your Database
Similar to your site, you also need a plot of land to lay the foundation for your database on. Your site will be able to access it even though it’s in a different location; it just needs to know where. In these steps we will create a database in Azure, get its address called the “connection string” and configure our project to use the database at that address.
- In the Azure Portal, click “SQL Databases” click the + button on the bottom left to create a new one.
- Click “Custom Create” and give the database a name and use your free trial for the subscription. Leave everything as default, but make sure Server is “New SQL database server.”
- Click on your database and click “View SQL Database connection strings for ADO.Net, ODBC, PHP, and JDBC”. This will open up a window with many types of connection strings, but we only need the ADO.NET one. Copy this string.
- Go into Visual Studio and paste this new connection into your project’s web.config. Make sure to remove the old connection string you were using for your local SQL Server database.
- Publish your website so that it gets this updated web.config. Your Azure site now uses your Azure SQL Server database.
Using a code first implementation of your database means that your C# code controls what happens to your database. Depending on where you specify database initialization (in the web.config or Global.asax.cs’s Application_Start method), take note on the type of initializing its doing. Usually it may be “DropCreateDatabaseIfModelChanges”, “DropCreateDatabaseAlways”, or a custom one you created that implements one these initializers. If you find yourself publishing frequently, beware of how often your initializer drops and recreates your database. If this happens too many times, it could potentially lock your host up and cause weird things to happen.
That is all you should need for basic deployment of your site and database! These were the core steps you need to get a simple .NET website and SQL Server hosted, but there is a good chance this might not be everything you are looking for. Below I will go over some additional procedures that are commonly paired with the above. I will be going over membership accounts and custom domain names as well as a quick overview of Microsoft’s Bizspark program.
If you are using .NET’s membership provider that Contoso University uses (this allows users to create an account with a password) you will need to configure your brand new SQL Database to use these features. If you set your application up similarly to Contoso University’s, you will have 2 SQL databases: 1 for your main application data and 1 for storing information about user accounts that .NET’s membership provider uses.
To do this, create an additional SQL Server database on Azure using the above procedure. In the web.config, make sure to replace the connection string that is being used by the membership provider. After that, follow the instructions in this guide under the “Resolution” header.
Custom Domain Names
Do you want to break away from that pesky “.azurewebsites.net” in your url? This guide shows you how to forward a purchased domain name to your Azure website. This article uses GoDaddy as the domain provider but the process is similar for others as well.
Are you part of a Startup? Microsoft Bizspark is the answer
Microsoft Bizspark is a program that provides discounted/free software services to startup companies. There are a few guidelines, but essentially all you need is a domain name with a simple splash page and a @company_name email address. You can buy both of these things from any web hosting company such as www.godaddy.com or www.networksolutions.com.