Introducing ASP.NET Web Pages 2 - Publishing a Site by Using WebMatrix
This tutorial is the final installment in the tutorial set that introduces ASP.NET Web Pages and Microsoft WebMatrix. It discusses how to publish your site to the Internet so that others can work with it.
Level: Beginner to ASP.NET Web Pages.
ASP.NET Web Pages 2
Completed tutorial through Creating a Consistent Look for ASP.NET Web Pages Sites
Downloads: Completed website for the ASP.NET Web Pages introductory tutorial
What you'll learn:
- How to publish your site to the Internet.
- What's coming up in the next tutorial set.
About Publishing Your Site
Up to now, you've done all your work on a local computer, including testing your pages. To run your .cshtml pages, you've used the web server that's built into WebMatrix, namely IIS Express. But of course no one can see the site you've created except you. To let others work with your site, you have to publish it to the Internet.
Unless you have access to a public web server already, publishing means that you have to have an account with a hosting provider. A hosting provider is a company that owns publicly accessible web servers and that will rent you space for your site. Hosting plans run from a few dollars a month (or even free) for small sites to many hundreds of dollars a month for high-volume commercial websites.
Note You might have access to a public web server via the internet service provider (ISP) that you use to get internet service at home. However, your hosting provider must support ASP.NET Web Pages. Many ISPs don't, but it's always worth checking.
In this tutorial, we'll give you an overview of how to publish. It's not practical to provide exact details for everything, because the process differs a bit for every hosting provider. But you'll get a good idea of how the process works.
Selecting a Hosting Provider
The first step is to find a hosting provider. You can look for one by searching the web or right from within WebMatrix.
In the WebMatrix ribbon, click the Publish button.
The Publish Settings dialog box is displayed.
Click the Find web hosting link.
You go to a page on the Microsoft site that lists hosting providers that support ASP.NET.
Obviously, it can be difficult to know now exactly what hosting features you might require over the long term. Here are a couple of things to consider:
For purposes of the WebPagesMovies site, you don't have to have a separate add-on for SQL Server, which often costs extra. In your site, you're using SQL Server Compact Edition, which is self-contained. However, you might need SQL Server access for some future website work you do. If you think you might, make sure that you can add SQL Server capability later.
Check whether the hosting provider supports the Web Deploy publishing protocol. You can publish by using FTP protocol, but it's more convenient to use Web Deploy.
Some sites offer a free trial period. A free trial is a good way to try publishing and hosting while you're still experimenting with WebMatrix and ASP.NET Web Pages.
Pick one that you like. For this tutorial, we selected DiscountASP.NET, because while we were creating the tutorial, that company had a promotion that let people host a site free for a few months.
Note Our choice of a hosting provider for this tutorial shouldn't be interpreted as an endorsement of that company over any other. But we had to pick one for illustration, and DiscountASP.NET is one of the many companies that supports ASP.NET Web Pages and the Web Deploy protocol for publishing.
Typically, after you've signed up with the hosting provider, the company sends you an email that contains a user name and password, the URL of the web server, and so on. If the hosting company supports Web Deploy protocol, they might send you a file that contains publish settings, or let you download one. A publish settings file simplifies the process for you.
Publishing the Site
When you've signed up and are ready to publish, click the Publish button in the WebMatrix ribbon. The Publish Settings dialog box is displayed.
If the hosting provider sent you a publish settings file, click the Import publish settings link and import the file. If you don't have a publish settings file, fill in the fields by using the values that the hosting company sent you in email. Here's what the Publish Settings dialog box might look like when you're done:
Click Validate Connection. If everything is ok, the dialog box reports Connected successfully, which means it can communicate with the hosting provider's server.
If there's a problem, WebMatrix does its best to tell you what the problem is:
Click Save to save your settings. WebMatrix offers to perform a test to make sure that it can communicate correctly with the hosting site:
Click Yes. WebMatrix uploads some sample files to the hosting provider. When the compatibility test is done, WebMatrix reports the results:
If you're ready to go, go ahead and click Continue to start the publish process for real. WebMatrix figures out what files are in your site and are already on the host server (right now, none) and gives you a preview of the publish process:
The list of files to publish includes the web pages that you've created like Movies.cshtml. The list also includes files for helpers that you've installed, the files to run SQL Server Compact Edition for your database, and so on. As a result, the initial publish process can be substantial.
Click Continue. WebMatrix copies your files to the hosting provider's server. When it's done, the results are reported in the status bar:
To see your live site, click the link in the status bar. Add Movies to the URL, and you'll see the Movies.cshtml file that you created:
Updating the Live Site: Republishing
Once you've published your site, there are two copies of it — the version on your computer and the version on the hosting provider's server. You'll probably want to continue developing the site (if nothing else, as part of the next tutorial set). When you do, you have to republish your site in order to copy changes from your computer to the hosting provider's server. The publish process in WebMatrix can determine what files have changed on your site and publish just those files.
To see how republishing works, open the Movies.cshtml site, make some
small change, and then save the file. For example, change the title to
Movies - Updated.
Click the Publish button in the ribbon. WebMatrix determines what's changed and shows you a preview of the files it will publish.
Important By default, WebMatrix publishes your database (.sdf file) only the first time you publish the site. Once your site is published and people are interacting with the website, the database on the live site typically has the site's real data. You have to be very careful not to overwrite the live database with the .sdf file that's on your computer, which usually contains only test data. That's why you see the warning Publishing will overwrite any remote databases, and why the check box for WebPagesMovies.sdf is cleared by default.
Click Continue. WebMatrix publishes the changed files and shows you a success message, like it did the first time you published.
Go to the live site (you can click the link in the success message if it's still showing) and verify that your change has been published.
Preview of the Next Tutorial Set
In this tutorial you've created a site that's functional and that illustrates much of the functionality of ASP.NET Web Pages. But there's plenty more to learn.
The next tutorial set continues with your Movies site and teaches you the following:
How to start a site from a template, which instantly gives you a professional layout plus built-in functionality for common tasks.
More about how to work with forms — radio buttons, checkboxes, drop-down lists, and so on.
How to extend your database with more information and how to perform more sophisticated database searches.
How to add social media to your site — for example, adding a Facebook "Like" button.
How to begin adding client-side functionality to your site (by using jQuery), which gives the user a very responsive experience.
How to add images to the site, including images uploaded by users.
How to send email from your site.
How to add login security to your site, so that some functions (like deleting a movie) are available only to users who've been authorized.