How to add Web API and MVC to a LightSwitch 2013 project
This is an update for the RTM of Visual Studio 2013. Unfortunately Microsoft broke the scaffolding availability under LightSwitch projects. I believe this is because up until the RTM, the scaffolding was part of a separate install from asp.net. RTM included this as part of the core product. Some of the hazards of working with anything but RTM products.
There has been some adjustments from the first posts so make sure you read thru and don't take anything for granted. I've also put the code up on GitHub so as to try and keep the text to a minimum.
Visual Studio LightSwitch is a great product. But it's not the end all and most professional development projects require a mix of technologies to be considered successful. By following these steps, you'll end up with a LightSwitch project that has the HTML Client, Silverlight Client, MVC, Web API and Web Forms with the naming conventions of your choosing. Giving you a solution without restrictions!
First time thru the tutorial it may still take you 15 minutes. So give it a try and see what you think…
- Start Visual Studio 2013
- Create a new project: File, New Project…
- Select the LightSwitch Desktop Application, this is a change
- Name the project: myTest
- Name the solution: myTest
- Double click on the properties file for the myTest project
- Enable forms authentication, and allow Security Admin for debug
- Save the solution, do a full build
- Right Click on the myTest project, add Client, HTML Client
- Name the client: App
- Once the client has been installed
- Right click on the DesktopClient and select Remove
- Right click on the solution, Open folder in file explorer
- Delete the DesktopClient folder
- In your server project, Data Sources, Create a Table
- Name it: TestItem with 1 property, Name
- In your App project, create a browse screen with TestItems
- Include an add/edit screen so you can verify data gets saved
- Build the app, run, and add some data
- Close your browser, and back into Visual Studio
- Go to GitHub and download the source files from here
- Back in Visual Studio
- Open up VS Package Manager, Tools, Library Package Manager, Package Manager Console
- Make sure the selected Default Project in the console is your server project
- Run the following command: install-package Microsoft.AspNet.Mvc
- Run the following command: install-package Microsoft.AspNet.WebApi
- Run the following command: install-package Microsoft.AspNet.Web.Optimization
- Open up the web.config file in the root of your Server project
- From the zip file from GitHub, add the code from the web.config.merge.txt file into your web.config
- Pay attention to the comments in the txt file on where to paste it in your web.config
- Also from the GitHub zip, copy the other folders into the root of your project
- In Visual Studio, show all files, refresh, to show all the folders you copied
- Right click on the new folders and include in your project
- Add a Global.asax file to the root of your project
- Copy the code from the Global.asax.Merge.txt file into your new Global.asax file
- Add the following using statements in your Global.asax
- System.Web.Mvc
- System.Web.Http
- System.Web.Routing
- Finally, in the root of your server project open the default.aspx file
- Remove the first line with the <%@
- Select Build, Clean Solution
- Select Build, Rebuild Solution
- Run your app
Moving on, let's test access to the ApplicationData.svc file http://localhost:{port}/ApplicationData.svc
So far so good eh? How about our MVC? http://localhost:{port}/ http://localhost:{port}/Home
Yay! Looking good… now our default.aspx file in the root… http://localhost:{port}/default.aspx
Yep that works too… final test… the Web.Api side… http://localhost:{port}/api/Test
You can now go in and clean up the project, delete the test screen, test table, etc. Save the solution again.
This would be the point when you would add your project into source control.
Going forward, we will be having all of our Web projects starting with this base. We are able to create a full MVC website, Desktop SPA application and our LightSwitch Mobile app all within the same project and sharing code.
One of the great features out of this is we can use Visual Studio LightSwitch to do all of our data management without having to go down into the Entity Framework.
Next post we'll dig into User Management with an HTML interface vs. Silverlight.
Happy coding!