I was looking to create a simple composite UI style application in Microsoft MVC3. What I want is a host MVC application that will compose its UI from other Controllers and Views packaged in other DLLs. The concept comes from autonomous components and SOA where a service owns not only its traditional business services but all the data and almost-all of the UI.
I thought I would be overriding controller builders, adding configuration, etc, etc, but actually this “poor-mans” variant is very easy. Here are the steps I followed;
- Create your Host MVC3 web application (Host.proj), for the sake of this example choose the Internet application so you get the default HomeController
- Create your satellite MVC3 controller by creating a standard class library project, called Satellite.proj
- Add a reference in Satellite to System.Web.Mvc
- Create a controller for your satellite, say MyController. Add an ActionResult such as;
public class MyController : Controller
public ActionResult MyInformation()
- Create the View folders for your controller in the Satellite project, e.g. Views\My\
- Now for linking them together. I just added a project reference from Host to Satellite in order to grab the DLL. The Views are slightly harder, I used a post build event in the Satellite such as; xcopy “$(ProjectDir)Views” “$(SolutionDir)Host\Views” /E /R /Y
- That’s it. Just add some code to use the controller, such as placing the following in the index view;
Edit: Note you get errors complaining that ‘there is no build provider registered for the extension’ you need add a dummy web.config to the project, follow these instructions;
Using Razor in a class library
A quick “tip”. Currently Visual Studio C# 2010 Express cannot install the NuGet extension. To workaround the problem you must have Visual Studio Web Developer Express. The NuGet extensions has been signed to work with VWD. Then to install a NuGet package for your VCE project do the following (it’s not nice);
1. Create your project in VCE, save and close
2. Open the project in VWD, it will probably complain but just ignore it
3. Run your NuGet command, e.g. Install-Package
4. Save and close VWD
5. Reopen VCE, add reference and browse to the packages folder that is now in your solution.
There, it’s not nice but it does work
Over the past couple of weeks I have been trying to use the Visual Studio 2010 Performance Analyizer on a web site but kept on getting;
VSP 7008: ASP.net exception: “The web site could not be configured correctly; getting ASP.NET process information failed. Requesting ‘http://localhost/VSEnterpriseHelper.axd’ returned an error: The remote server returned an error: (404)
The site is a relatively complex IIS6 site with a number of smoke and mirror redirects so I guessed this complexity was confusing the tools. I contacted Microsoft Support to help. During a shared-desktop session with support it started working! Ah the curse of the support person making it work just by looking at it. What actually happened is that when I launched the Performance Wizard I incorrectly gave it a file URL (\\localhost) rather than a web URL. The wizard prompted to say there were no DLLs to profile but I said to continue (not realising my mistake). The profile finished with no results. I corrected the problem (//localhost) by editing the resulting profile NOT by creating via a new wizard. It works?! So I had created a workaround . I then manually added the missing target DLLs and re-ran the profile. No problems, everything worked.
There you go, if you are running IIS6 and having problems getting the Profiler to connect, why not try this ridiculous workaround.
I’ve had a quick play with adding the contents of a folder from another project, i.e. I want to add links to all the files in another project’s folder. So for example; I have a project called ConsoleApplication1 and I want to ensure it always has links to the images in another project called ClassLibrary1 (yes nice names eh). So first create a folder with the same name, e.g. images, in ConsoleApplication1. Add a link to one of the files in ClassLibrary1\Images (Add existing item->add as link). Unload ConsoleApplication1 and Edit the project file. Locate the Content Include and replace it as follows;
So whatever changes you make to ClassLibrary1\Images will automatically be reflected in ConsoleApplication\Images. It’s early days but it seems to run ok.