C# – Running an MVC Application as a Sub-Application

asp.netasp.net-mvcciis-7net

I'm attempting to create an MVC application as a sub-application to my standard Asp.Net Web application. Both of these projects are inside the same solution. While the parent application appears to be going fine, I'm having trouble getting the sub-application to work. After some massaging of my two web.configs, I was able to get the Asp.Net runtime to accept the configurations, but I have been unable to browse to any of the pages/controllers in the MVC application, including the root of the sub-application ("http://RootSite/SubApplicationName/"). I continually get 404's.

Actually, I do get a response when going to the url "http://RootSite/SubApplicationName/Home/Index/". It redirects me to index.aspx in that folder, and throws this error:

The view 'Index' or its master could not be found. The following locations
were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx

The sub-application in IIS (7) is set up fairly straight forward: it's set to run in the same application pool as the parent app, which runs Asp.Net 2.0 in integration mode.

My suspicion is that I have something in the web.configs that is throwing it off. Are there things regarding, say, HTTPModules or URL authorization modules, etc., that I should confirm aren't getting in the way of MVC?

Also, in the global.asax.cs file, should the default route be different? By default, the url parameter passed to routes.MapRoute is:

"{controller}/{action}/{id}"

Should it be preceded by the name of the sub-application, like so?

"SubApplicationName/{controller}/{action}/{id}"

I attempted a change like that, but it did not fix things.

Any ideas are much appreciated. Also, general information about setting up an MVC web application as a sub-application would be great.

Thanks.

Best Answer

I did something similar, however not the same, I had to load views from a separate dll. In my case it was a class library, not a different web app, but it should work the same as far as I know.

The first thing you have to do is to create a VirtualPath Provider to tell the routing engine how to look for your stuff in the subapplication views. A great explanation of how to do this can be found here:

http://www.wynia.org/wordpress/2008/12/05/aspnet-mvc-plugins/

I'm sure that will get you started ;)