How To Deploy & Publish a .NET MVC Application

This is the second post in the series called .NET MVC From a PHP Developer’s Perspective where I discuss the pros and (mostly) cons of jumping onto the .NET bandwagon. Keep in mind that I’m a php/unix guy. I’m now at week 5 and I recently deployed my .NET MVC application to a web hosting provider. This was done for a testing and debugging purpose, since I had to become familiar with the whole process.

We begin with picking the right hosting plan for your .NET MVC application, then proceed to deploying your application and the contents (images, javascript, etc). Finally I show you a little trick on how to deploy your database in a format (.bak) most hosting providers will (hopefully) accept.

Picking the Right Hosting Plan

I picked Parking.ru – a Russian Windows hosting provider, which were generous enough to provide me with a two weeks trial period. My choice was based on a few calls to several different hosting providers, their technical teams had no clue about if my .NET Framework 4, MVC 2.0 application with an SQL Express database would run on their hosting plans. The answers were all similar to:

Well we’re not sure, you may try this hosting plan, but we’re not sure. And you still have to pay in order to use it. It’s running Windows Server, but we don’t know what MVC is, so we can’t guarantee you anything, sorry.

Heh, there was always a problem with Russian hosting providers, but overall the service is getting better. Tech teams now at least know whether they’re offering Apache or nginx ;) But anyways, I was quite happy to get a free trial at Parking.ru, and since they had no FAQs on how to publish my MVC application, I ran off to Google.

Publishing Your Application and Contents

It’s not really a big deal after you get it the first time. Visual Studio can automatically publish your application and the contents to a remote FTP server, which in my case worked like a charm. The only prob I had is with the external contents such as images, stylesheets and some javascript, which all sumed up to around 500 files. I didn’t add them as resources to my project, thus they were never published. Some common sense was required here, so I fired up my favorite FTP client and flushed them all manually.

Publishing Your SQL Express Database

The major issue was of course with the database. As I wrote in my previous article, the MVC Music Store is the tutorial my application was based on. The tutorial creates the models from a database scheme, and all the data is stored in an SQL Express file with the MDF extension. My hosting provider supported backups (.bak) which I could upload to their servers, so I googled around for ways to generate such backups.

Most articles I came across mention SQL Server Management Studio, so I installed that, and after a few trial and error, I managed to get my SQL Express database inside the Studio. Now what’s weird is that the backup option didn’t work, yielding something about incorrect filenames. Of course my database was called [C:UserskovsheninDocumentsVisual Studio 2010…], but SQL queries worked quite okay, so did an SQL export which generated an SQL dump of all my data. Unfortunately that was not good enough for my hosting provider.

With a few more experimenting and searching, I came across the following query, which could be issued within Visual Studio itself (does not require the SQL Server Management Studio). It generates a .bak file which is exactly what I was looking for:

BACKUP DATABASE [C:PATHTOYOURDATABASE.MDF] TO DISK = N'C:my-database.bak' WITH NOFORMAT, NOINIT,  NAME = N'My Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10

Honestly, I have no idea what some of the options mean, but I’m sure Google can help. One thing you should pay attention to is the square brackets in the database name, especially if it contains the space character (which it will, if you saved your database in the Visual Studio directory by default). The command could not be parsed by Visual Studio, but the execution generates a .bak file on your hard drive. I instantly uploaded the file to my hosting provider, and voila!

I spent 3-4 hours in total on deploying my MVC application, but now that I got it right, takes me a couple of minutes. Then again, I really miss mysqldump, subversion and the rest. They seem to be a little more lightweight than .NET and IIS.

About the author

Konstantin Kovshenin

WordPress Core Contributor, ex-Automattician, public speaker and consultant, enjoying life in Moscow. I blog about tech, WordPress and DevOps.

3 comments