Web Developers today build and test ASP.NET sites and applications using one of the two web-servers:
- The IIS Express that comes built-into Visual Studio
- The IIS Web Server that comes built-into Windows
If you have noticed the URL displayed in the browser shows something like http://localhost:15599 when running your app within Visual Studio. The integer value in the URL represents the port number used in IIS Express. IIS Express is the default web server for web application projects in Visual Studio 2012 and higher versions. The default internal web server in Visual Studio typically used to build and run your app during development for you to test and debug codes.
Taken from the official documentation: IIS Express is a lightweight, self-contained version of IIS optimized for developers. IIS Express makes it easy to use the most current version of IIS to develop and test websites. It has all the core capabilities of IIS 7 and above as well as additional features designed to ease website development including:
- It doesn't run as a service or require administrator user rights to perform most tasks.
- IIS Express works well with ASP.NET and PHP applications.
- Multiple users of IIS Express can work independently on the same computer.
You can see the IIS Express configuration by right clicking on the project and then by clicking on the “Web” tab. The figure below shows how it looks like:
Figure 1: Web Settings
You will use IIS Web Server when you want to test your web application using the server environment that is closest to what the live site will run under, and it is practical for you to install and work with IIS on your development computer. This section will walk you through on how to deploy and host your ASP.NET MVC 5 web application in your local IIS Web Server.
Typically websites will be deployed within wwwroot folder under initpub but there are times that you may need to put your website source outside of wwwroot folder. This article will show you how to add a virtual directory in IIS so you can point the location to where your source is hosted.
In this article I’m going to use the project that I’ve demonstrated from this article: Building Web Application Using Entity Framework and MVC 5
Before deploying your app, verify that you have IIS installed in your machine. If you already have IIS installed then you can skip this step otherwise if you don't then just follow through.
In this particular project I used Windows 8.1 as my Windows Operating System. If you are using a different OS version of Windows then I'm sure there are plenty of resources from the web that demonstrate the installation of IIS in your Windows machine.
Installing IIS8 on Windows 8.1
Open Control Panel and click on “Programs” as shown in the figure below.
Figure 2: Windows Control Panel
Then click on “Turn Windows features on or off” from the Programs and Features dialog and select “Internet Information Services” from the list as shown in the figure below.
Figure 3: Windows Features dialog
Expand Internet Information Services (IIS) and check/enable all components under “World Wide Web Services” > “Application Development Features” as shown in the figure below.
Figure 4: Windows Features dialog
Click “OK” to let Windows install the required files. Once installed you may now close the dialog. Now open an internet browser and type-in “localhost” to verify that IIS was indeed installed. It should bring up the following page below:
Figure 5: IIS page
Publishing from Visual Studio
If you’ve seen that in the browser then we are ready to deploy and host our app in IIS. Now switch back to Visual Studio 2015 and then right click on your project, in this case “MVC5RealWorld” and then select “Publish”. It should bring up the following dialog below.
Figure 6: Publish Web dialog
Select “Custom” from the options and enter a profile name for your host as shown in the figure below:
Figure 7: New Custom Profile dialog
Click “OK” to bring up the following dialog below.
Figure 8: Publish Method
Now select “File System” as publish method and enter your preferred deployment location. In my case I target it at this location “C:\Users\ProudMonkey\WebSite” in my local drive. See the figure below for your reference.
Figure 9: Publish Method
Click “Next” and then select “Release” as the configuration and check the “Delete all existing files prior to publish” option to make sure that Visual Studio will generate fresh files once you re-publish your app.
Figure 10: Publishing
Click “Next” and it should take you to the next step where it will inform you that your web app will be deployed to the location you supplied from the previous step. If you are sure about it then just click “Publish” as shown in the figure below.
Figure 11: Publishing
Visual Studio will compile and publish your app to the desired location. When succeeded then it show something like this in the output window.
Figure 12: Publish succeeded
Now browse the location to where you point your files to be published. In this example the file was published in “C:\Users\ProudMonkey\WebSite” (it could be a different location in your case). To verify that the location is accessible in IIS then make sure that the folder containing the published files is not “Read-Only” and has proper access permissions. You can verify it by right-clicking on the folder and see the read-only option. Make sure it is unchecked.
We’re Not Done Yet!
Converting Your App to Web Application
Yup, we’re not done yet. The last step is to configure IIS to convert your app as a web application. To do this open IIS Manager or simply type “inetmgr” in Windows 8 search box. It should bring up the following window below.
Figure 13: IIS Manager
Expand the “Sites” folder and then right click on the “Default Web Site” and select “Add Virtual Directory”. You should be able to see the following dialog below.
Figure 14: Add Virtual Directory
Enter an alias name and then browse the location where you publish the source files for your web app. Again, in this case “C:\Users\ProudMonkey\WebSite”. Now click “OK”. The “MVC5Demo” folder should be added under “Default Web Site”.
Now right click on “MVC5Demo” folder and select “Convert to Web Application”. It should bring up the following dialog.
Figure 15: Add Application
Click “OK” to convert your folder into a Web Application.
Enable File Sharing in IIS
Now to ensure that our virtual location to where we publish the web site is accessible to IIS then we need to enable “Sharing” so IIS users can have access it. To do this, right-click on “MVC5Demo” and select “Edit Permissions”. In the dialog click on “Sharing” tab and click the “Share” button. It should bring up the following dialog below.
Figure 16: File Sharing
Add “Everyone” and click “Share” to add it to the list. You should now be able to see the something like below after you’ve added the users.
Figure 17: WebSite Properties
Now open up internet browser and try to access this URL: http://localhost/MVC5Demo/Account/Login
It should show up the Login page just like in the figure below:
Figure 18: Hosted App in IIS
Now try to enter an account credentials and click “Login”. If you are seeing the following error below, don’t panic! :D
Figure 19: Cannot open database error
To fix the error, you need to create a login for “NT AUTHORITY\SYSTEM” in your SQL Server and give it the necessary permissions that your app requires.
Configuring SQL Server Logins
Open SQL Express Management Studio as an “Administrator” and navigate to Security > Logins > NT AUTHORITY\SYSTEM as shown in the figure below.
Figure 20: Configuring Logins
Right click on “NT AUTHORITY\SYSTEM” and select Properties. Select “Server Roles” from the left panel and make sure that “public” and “sysadmin” are checked as shown in the figure below.
Figure 21: Login Properties
Configuring Application Pool’s Identity
Now open IIS Manager. Select “Application Pools” and select “DefaultAppPool” from the list since our app uses this default application pool. If you are using a different application pool for your app then select that instead. On the left panel, select the link “Advance Settings” as shown in the figure below.
Figure 22: AppPool Advance Settings
Make sure that you select “Local System” as the Identity from the Advance Settings dialog as shown in the figure below.
Figure 23: Advance Settings
Click “OK” and try to browse your page again using the same URL.
Running Your Application
You should now be able to connect to your database. Here are some screen shots of the page hosted in IIS.
Figure 24: After Successful Login
After updating the database
Figure 25: After successful update to the database
That’s it. You now have a web app hosted in IIS Web Server that is up and running. :)