Configuring and deploying to IIS
Perhaps the most common ASP.NET
MVC application hosting scenario involves creating and configuring a website using Internet Information Services, or IIS. The good news is that ASP.NET
MVC applications are for the most part just like any other ASP.NET
application. So if you're already familiar with deploying an ASP.NET
web application to IIS, you don't have much to learn and none of these steps should come as a surprise to you. If this is your first time working with IIS websites or ASP.NET
applications, however, don't worry.
In this video I will walk you through everything you need to know in order to get started. Before we can create and deploy our website, we first need to ensure that the target web server has the .NET framework installed, version 4.0 or later. As long as the .NET framework is in place, creating a new IIS website is a very straightforward process. To begin, create the directory that your website will be hosted from. For example, inetpub/wwwroot/MvcAuction in the C: drive.
Then open up the IIS management application, Internet Information Services (IIS) Manager, right-click on the Default Web Site and choose the Add Application menu option to display the Add Application dialog. In this dialog choose whatever name you'd like to give your website, for example, MvcAuction. Then give it the path to the directory that you just created in the first step. Before we leave this dialog, let's check something.
Click the Select button next to the Application pool field to pop up the Select Application Pool dialog. Here check to make sure that the Application pool that you use has the .NET framework version 4.0 or later. If the Default Application pool is not configured to use version 4.0 of the .Net framework, choose or create a New Application pool that does use .net 4.0. Finally, click OK to have IIS create your website.
Now you have a website that you can deploy your application to. Once you have your IIS application created and configured, you have several deployment techniques at your disposal. The most accessible deployment technique is Visual Studio's built-in publishing mechanism. To use it, right-click on the ASP.NET
MVC project and choose the Publish option from the Context menu to open the Publish web wizard. To create a new publishing profile that will allow you to deploy your website, select the New option from the dropdown list in the Profile tab and give the new profile a name, for example, Local IIS Website.
Then since we're deploying to the local file system, select the File System option from the list of available Publish methods. The File System Publishing option is only suitable if you're deploying to a web server that you have direct File System access to via your network. If you're using a web hosting service this is probably not the case, so you'll have to choose one of the other publishing methods to deploy your website. Contact your web host to see which of these methods they support as well as the configuration information they require.
Once you choose a Publish method, the dialog will change to allow you to fill in the rest of the configuration information needed for Visual Studio to publish using the selected method. When you've configured all of the required Publish method options, click the Publish button and Visual Studio will deploy your site to the location you specified. At this point, if you navigate to your newly deployed website in a browser, you'll most likely receive a timeout error.
We're getting this error because IIS is not able to reference our LocalDB database by default. At this point you have two options. The first is to use any other database server, preferably a full-fledged SQL Server Database instance. This is hands down the most ideal solution. But if you don't have access to a SQL Server, there are a few things we can do to get IIS talking to LocalDB. First open up the Visual Studio command prompt as an administrator.
Then execute this command to place LocalDB into Shared mode so that IIS can access it. Once we've done this, we'll need to change the site's connection strings to point to this shared instance. Now publish this site again with these updated settings. This time when you try to run your site, you should be prompted with an access denied error. My site works fine because I've already run the commands that I'm about to show you.
But should you receive this access denied error, here is how to fix it. First, go into Visual Studio and open a connection to LocalDB using the Database Explorer tab. For the server name, enter the value that we're using in our configuration file. Then right-click on this new connection and click New Query, enter the following code.