Using Visual Studio to create a new Office Add-In results in two projects: One for your Office Add-In (basically, the manifest) and another for the web project where you do the bulk of your work to implement functionality. The web project that is created is a basic HTML/JS/CSS application…nothing fancy like ASP.NET. For most situations, a lightweight client-side web application is ideal and it makes sense for that to be the default of the VS project template. How about those other situations where you need something to run on the server, like an ASP.NET MVC application? There are a couple choices:
- Add a new MVC project to your solution and pull in the Office “stuff” and trim it down so it can be used as the web project for your Add-In
- Convert the existing web project to MVC
I’ll show you how to do the second option in this post.
- Using NuGet Package Manager, add Microsoft.AspNet.Mvc to your web project
- Add the following folders to your project: App_Start, Controllers, Views
- Right-click the Views folder and add a new Web Configuration File and name it Web.config
- Add the following code to this new Web.config, replacing [[YourNamespace]] with the namespace of your project
I chose to use the Site.css stylesheet that the NuGet package created for me. To do that, I took all of the CSS from the app.css and the home.css file and put it in the Site.css file.
Copy the App.js and Home.js files to the Scripts directory.
I’ll assume your existing web project has the standard files from the VS template: app.js, app.css, home.html, home.css and home.js. This section shows how to pull that content into a new view.
- Right-click the Controllers folder and add a new Controller named HomeController. This should add a file that looks like the following:
- Add a Home and Shared folder to the Views folder
- Right-click the Shared folder and add a new View called _Layout using the “Empty (without model)” template and checking the box for “Create as a partial view”
- Replace the <head> content with the following (note the reference to the Office UI Fabric, that’s optional if you aren’t using it…but you should be):
- Replace the <body> content with the following:
- If you have anything like a top nav or other content that is consistent across multiple pages in your app, paste it in there as appropriate
- Right-click the Home folder and add a new View called Index (or whatever you want it to be called, the rest of this post assumes Index) using the “Empty (without model)” template and checking the box for “Create as a partial view”
- Replace the contents of index.cshtml with the HTML from the <body> section of your home.html file (keep the ViewBag.Title at the top of the file if you’re going to use it)
Config and Cleanup
- Right-click the Views folder and add a new partial view called _ViewStart and add the following content:
- (This may already exist but create it if not) Right-click the App_Start folder and add a new class file called RouteConfig.cs and add the following content:
- Open the Global.asax.cs file and edit the Application_Start() method to the following:
- If your app doesn’t use Bootstrap, delete the Bootstrap CSS and JS files (if you are, go back to your _Layout.cshtml file and add the Bootstrap references as the above code doesn’t have it)
- You may have noticed the MVC package brought in a reference to a newer version of jQuery (1.10.2 as of the writing of this post.) There are now probably two jQuery versions in the Scripts folder so delete the version you don’t want.
- Open the App Manifest XML file (from the Office Add-In project) and set the DefaultValue for SourceLocation to ~remoteAppUrl since the default page of the app is now the web app default page
That should do it. Please post a comment here or contact me directly if you hit any snags.