Set Up Server Environment For ASP.NET MVC Application On Development Machine

Introduction

This article introduces how to set up server environment for ASP.NET MVC application on local development machine. As a web application hosts on server accessible by end user so it is good practice that we develop application in the same live environment on a local system so that we don’t need to do more changes at the time it goes live. An MVC application may host and test one of the two web servers.

  1. IIS Express

    Its default internal web server in Visual Studio is typically used to build and run your application during development for you to test and debug codes. It 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.

  2. IIS Web Server

    It comes built-into Windows. It provides 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 machine. This article will walk you through how to publish and host your ASP.NET MVC application in your local IIS Web Server and debug hosted application in Visual Studio.

Install IIS Web Server

As IIS Web Server built-into Windows OS so it’s easy to install in few steps which are as follows in Windows 8.1:

  1. Open the ‘Control Panel’ and click on Programs Setting.

  2. There is another new ‘Program’ window opens and click on ‘Turn Windows feature on or off’ under Programs and Features section.

  3. Now ‘Windows Features’ window opens and expands ‘Internet Information Services’ node.

  4. After that expands child ‘World Wide Web Services’ node and check/enable all components under ‘Application Development Features’.

  5. After that click ‘OK’ button 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:

    IIS Web Server on local machine
                                  Figure 1: IIS Web Server on local machine

Star Rating in MVC Application

We create an ASP.NET MVC application which we used to set up on IIS web server. We develop star rating using css in this example so we create an empty MVC project in Visual Studio. We create a view model to pass data from view to controller and vice-versa as per following code snippet.

  1. namespace StarRating.Models  
  2. {  
  3.     public class RatingViewModel   
  4.     {  
  5.         public int Rating   
  6.       {  
  7.             get;  
  8.             set;  
  9.         }  
  10.     }  
  11. }  
We create a Rating controller which has GET and POST action method as per following code snippet.
  1. using StarRating.Models;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace StarRating.Controllers   
  5. {  
  6.     public class RatingController: Controller   
  7.     {  
  8.         [HttpGet]  
  9.         public ActionResult Index()   
  10.         {  
  11.             RatingViewModel model = new RatingViewModel();  
  12.             return View(model);  
  13.         }  
  14.   
  15.         [HttpPost]  
  16.         public ActionResult Index(RatingViewModel model)   
  17.         {  
  18.             // Validation & Database opertions goes here  
  19.             return View(model);  
  20.         }  
  21.     }  
  22. }  
We write css for rating star design on UI as following code snippet.
  1. .rating  
  2. {  
  3.     unicode - bidi: bidi - override;  
  4.     direction: rtl;  
  5.     font - size: 30 px;  
  6. }  
  7.   
  8. .rating span.star, .rating span.star   
  9. {  
  10.     font - family: FontAwesome;  
  11.     font - weight: normal;  
  12.     font - style: normal;  
  13.     display: inline - block;  
  14. }  
  15.   
  16. .rating span.star: hover, .rating span.star: hover  
  17. {  
  18.     cursor: pointer;  
  19. }  
  20.   
  21. .rating span.star: before, .rating span.star: before  
  22. {  
  23.     content: "★";  
  24.     padding - right: 5 px;  
  25.     color: #BEC3C7;  
  26. }  
  27.   
  28. .rating span.star: hover: before, .rating span.star: hover: before, .rating span.star: hover~span.star: before, .rating span.star: hover~span.star: before   
  29. {  
  30.     content: "★";  
  31.     color: #41CAC0;  
  32. }  
  33.   
  34. .rating span.star.active::before, .rating span.star.active::before, .rating span.star.active ~ span.star::before, .rating span.star.active ~ span.star::before  
  35. {  
  36. color: # 41 cac0;  
  37.     content: "★";  
  38. }  
Now write the view on which rating star shows as per following code snippet.
  1. @model StarRating.Models.RatingViewModel  
  2. <div class="row">  
  3. <div class="col-lg-6">  
  4. @using (Html.BeginForm("Index", "Rating", FormMethod.Post, new { @class = "form-horizontal"role = "form" }))  
  5. {  
  6. <h4>Star Rating</h4>  
  7. <hr />  
  8. <div class="form-group">  
  9. <label class="col-lg-2 col-sm-2 control-label" for="Rating">Rating</label>  
  10. <div class="col-lg-8">  
  11. <span class="rating">  
  12. @for (int i = 1; i <= 5; i++)  
  13. {  
  14. var starClass = "star";  
  15. if (Model.Rating == 6 - i)  
  16. {  
  17. starClass += " active";  
  18. }  
  19. <span data-value="@(6 - i)" class="@starClass"></span>  
  20. }  
  21. </span>  
  22. </div>  
  23. @Html.HiddenFor(m => m.Rating)  
  24.   
  25. </div>  
  26. <div class="form-group">  
  27. <div class="col-md-offset-2 col-md-10">  
  28. <input type="submit" class="btn btn-default" value="Submit" />  
  29. </div>  
  30. </div>  
  31. }  
  32. </div>  
  33. </div>  
  34. @section Scripts{  
  35. @Scripts.Render("~/Scripts/rating-index.js")  
  36. }  
We write a JavaScript function to assign selected star value to hidden field so that we get this value on POST action on controller. The following code snippet is for the same.
  1. (function($)  
  2.  {  
  3.     function RatingIndex()   
  4.   {  
  5.         var $this = this;  
  6.   
  7.         function initialize()  
  8.     {  
  9.             $(".star").click(function()  
  10.              {  
  11.                 $(".star").removeClass('active');  
  12.                 $(this).addClass('active');  
  13.                 var starValue = $(this).data("value");  
  14.                 $("#Rating").val(starValue);  
  15.             })  
  16.         }  
  17.         $this.init = function()   
  18.         {  
  19.             initialize();  
  20.         }  
  21.     }  
  22.     $(function() {  
  23.         var self = new RatingIndex();  
  24.         self.init();  
  25.     })  
  26. }(jQuery))  
Now we open project properties by right clicking on project. The default web server is set IIS Express with URL in the Web tab of properties as shown in figure 2. It means when we run this application from the Visual Studio then it build and run at local IIS Express server.

IIS
                            Figure 2: IIS Express setting for web


After that we run the application from the visual studio and output shows as per figure 3.

IIS
Figure 3: Output of the Star Rating Application


Deploy Application on IIS Web Server

We publish and host the web application on our development machine IIS Web Server so that we can set up same live environment on local development machine. To deploy an application on IIS Web Server we divide process in two parts one is publish and another is host.

Publish ASP.NET MVC Application from Visual Studio

To publish ASP.NET MVC project from Visual Studio, we follow following steps one by one.

 

  1. Build ASP.NET MVC project /solution in Release mode.

  2. Right click on ASP.NET MVC project and click on “Publish” menu.

    publish
          Figure 4: Publish ASP.NET MVC Application

  3. Now  Publish Web pane opens, and choose profile tab from left tab. There is a “Select or import a public profile” in which you can either choose existing one or create new publish profile as shown in following image.

    Application
                      Figure 5: Publish Profile for ASP.NET MVC Application

    Now clicks on “Ok” and move on Connection tab in Publish Web.

  4. Now we choose File System in publish method and choose desired location in Target Location which we will be used to map IIS.

    Application
                   Figure 6: File System to publish ASP.NET MVC Application

  5. Now we choose configuration mode Release as shownin the  below figure and click on Next button. There are some options such as Delete all existing files prior to publish which means delete all existing files in target location, publish folder, and create new files.

    Configuration
                      Figure 7: Configuration mode for ASP.NET MVC setting

  6. Now we get publish preview which shows publish path and profile name. Now we click on publish button and all published files created at target location.

Host ASP.NET MVC Application on IIS Web Server

As we have published code so now we host this code on IIS step by step.

  1. Search inetmgr in search box and click on IIS manager icon.

  2. In the IIS manager, Right clicks on Sites under Connections pane as show in following image.

    iis
                                                    Figure: 8. IIS Manager

  3. Now we have following where we fill up all the information which is required to host ASP.NET MVC application on IIS Web Server.

Site name : Name of site

Application pool : .NETv4.5 (version of .net framework on which application run)

Physical path: Path of the published ASP.NET MVC application code and map to directory which have Web.config file.

Application
                        Figure 9: Application host on IIS server.


Now we set up binding for hosted application so that it can run on the 4.5 .NET Framework. You need to right click on hosted Website and expand Manage Website and click on Advanced Setting. The following image shows options for same.

Advance Setting
                  Figure 10: Advance Setting for Web

Now we set application pool for the application as shown in the following image.

Application pool setting
         Figure 11: Application pool setting in IIS

An IIS application pool is a grouping of URLs that is routed to one or more worker processes. Because application pools define a set of Web applications that share one or more worker processes, they provide a convenient way to administer a set of Web sites and applications and their corresponding worker processes.

Now we run the application using URL http://localhost/rating and get output same as figure 3.

As you can see we have still localhost in web URL to access the application so we make some more changes in configuration so that we can access it same as live site. There are follows:

  1. Right click on hosted web application.

  2. Click on the Edit Binding option in the pane and opens Site Binding popup.

  3. Select the website from Site Binding pop up and clicks on Edit button.

  4. Edit Site Binding popup opens and we assign a value ‘dev.rating.com’ to Host name field in it as shown in following figure.

    Assign Host name
                                        Figure 12: Assign Host name

  5. Now open your system hosts file from C:\Windows\System32\drivers\etc and make an entry for site host name as below
    127.0.0.1 dev.rating.com

Now we run the application using URL http://dev.rating.com/rating and get output same as figure 3.

Debug ASP.NET MVC Application

When we run application from the Visual Studio with IIS Express then debug process auto attach and hits breakpoint accordingly. Now we run hosted application from IIS Web Server using above mentioned URL. To debug the application we follows following steps:

  1. Click Debug option in Visual Studio menu.

  2. Select ‘Attach to Process’ option from drop down pane as shown in following figure 13.

    Debug process option
                                        Figure 13: Debug process option

  3. We attach w3wp.exe process to debug application as shown in below figure. Make sure application build configuration set is Debug. Set your breakpoint in application and debug accordingly.

    Attach Debug process
                                     Figure 14: Attach Debug process

Keep more articles on ASP.NET MVC in your treasure chest  at the  following link: ASP.NET MVC Cookbook.

Next Recommended Readings