Test your ASP.NET MVC or WebForms Application on IIS 7 in 30 seconds
A frequent complaint in the ASP.NET forums is that an application works fine with Cassini (AKA, the built in Visual Studio Development Server), but fails when deployed to a real IIS server. The two most common reasons for this failure are:
- Cassini runs at the root level, so when resources (such as CSS and JavaScript files) are referenced incorrectly, they still work. For example, create a new MVC 3 Application and change the CSS and jQuery as shown below:
@*<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>*@ <linkhref="/Content/Site.css""rel="stylesheet"type="text/css" /> <scriptsrc="/Scripts/jquery-1.5.1.min.js"type="text/javascript"></script>
This problem with assuming root access to the resources is so common that most ASP.NET MVC books and many ASP.NET MVC tutorials have samples that don’t work on IIS. - Cassini runs under the logged in users credentials, that’s you, and you're often running as administrator. IIS runs under the application pool identity, and for security reasons has far less access than an administrator. Access to any resource ( a remote SQL Server, a local file ) often fails when run on IIS.
IIS Express is a huge improvement over Cassini, but it doesn’t solve either of these problems. IIS has additional advantages over IIS Express:
- Easier to test your application from remote computers.
- Easier to set up and test SSL. See Better, Faster, Easier SSL testing for ASP.NET MVC & WebForms
Since IIS is tied to the OS, you need Vista/SP1 or higher to test with IIS 7. ( Win7/SP1 strongly preferred. )
Installing IIS 7
- Run the Web Platform Installer (WPI)
- In the left pane select All. In the tabs select Products.
- Install IIS 7 Recommended Configuration.
- In the Start menu, type IIS, and select Internet Information Services (IIS) Manager.
IIS manger is displayed.
Create a new ASP.NET MVC 3 application or Web Application, accept all the defaults. If you want to follow this tutorial, name it MvcFun.
WARNING: IIS cannot run an ASP.NET project that has been created in the default Visual Studio project folder (C:\users\<user>\Documents\Visual Studio 2010\Projects). Select a folder where IIS can access the files, such as C:\Webs.
Right click the solution and select Properties.
- Select Web in the left pane.
- Under Servers, select the Use Local IIS Web server radio button.
- Select the Create Virtual Directory button.
Should you get the message:
Unable to create the virtual directory. To access Web sites on the local IIS Web server, you must run Visual Studio under an Administrator account.
Read it and follow the directions and you’ll be rewarded with a friendlier message.
Now go back to IIS manager, refresh and drill down in the Default Web Site. Select Browse *:80(http) in the right pane.
If you get the following Parser Error, you need to build your application.
Server Error in '/MvcFun' Application.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: Could not load type 'MvcFun.MvcApplication'.
Source Error:
Line 1: <%@ Application Codebehind="Global.asax.cs" Inherits="MvcFun.MvcApplication" Language="C#" %>
Source File: /MvcFun/global.asax Line: 1
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225
Testing your Application
After building, refresh your browser. In the browser URL, change localhost to the name of your computer. In the image below, I’m using the Q3 computer.
Go to another computer and test the URL. If you cannot access the Web application from a remote computer, you might need to configure the firewall on the computer that is serving the Web pages.
To enable the Web server access through the Windows firewall
- In the Windows Search programs or files box, enter "firewall".
- Select Allow a program or feature through Windows Firewall.
- Click Change settings and scroll to the bottom.
- Select World Wide Web Services (HTTP).
- Verify that the URL for your ASP.NET MVC 3 application now works from a remote computer.