Learn How to Configure Lightswitch With MVC Part 3

This article provides a walkthrough of how to use MVC with Visual Studio LightSwitch.

Introduction
 
This article provides a walkthrough of how to use MVC with Visual Studio LightSwitch. In my previous articles we saw how to get started by creating a LightSwitch application. You can get them from the following:
 
 
Now let's continue by creating the remaining.
 
  • Step 1 Right-click on the server application and add a Global.asax file and use the following code.
 
 
 
The call passes the the Routes collection of the Global RouteTable as a parameter to the RegisterRoutes method, that then populates the routes collection with pre-defined route templates for the application.
 
  1. using System.Web.Mvc;  
  2. using System.Web.Routing;  
  3. namespace LightSwitchApplication  
  4. {  
  5.     public class Global : System.Web.HttpApplication  
  6.     {  
  7.         protected void Application_Start()  
  8.         {  
  9.             AreaRegistration.RegisterAllAreas();  
  10.             RouteConfig.RegisterRoutes(RouteTable.Routes);  
  11.         }  
  12.     }  
  13. }  
  • Step 2: Now Let's create a MVC controller under the Conrollers folder and add the following implementation.
 
 
 
 
  1. using System.Web.Mvc;  
  2. namespace LightSwitchApplication.Controllers  
  3. {  
  4.     public class HomeController : Controller  
  5.     {  
  6.           
  7.         public ActionResult Index()  
  8.         {  
  9.             return View();  
  10.         }  
  11.     }  
  12. }  
  • Step 3: Let's create a MVC view under the Views folder.
Right-click on the Views folder and add a Home Folder and create a MVC5 Razor view under it as shown below.
 
 
 
 
  1. @{  
  2.     Layout = null;  
  3. }  
  4. <!DOCTYPE html>  
  5. <html>  
  6. <head>  
  7.     <meta name="HandheldFriendly" content="true" />  
  8.     <meta name="viewport" content="width=device-width,  
  9.           initial-scale=1minimum-scale=1maximum-scale=1user-scalable=no/>  
  10.     <title>Welcome to Lightswitch with Model View Controller</title>  
  11. </head>  
  12. <body>  
  13.     <div>  
  14.         <h1>Hello from MVC!</h1>  
  15.         <a href="HTMLClient">LightSwitch Application</a>  
  16.     </div>  
  17. </body>  
  18. </html>  
Run the application. We will get the following output.
 
 
 
Summary

In this small article, I explained how to prepare a LightSwitch application to use MVC, so now we are able to follow the structure of MVC in Lightswitch..