3

I have a ASP.NET 4.0 web forms app and need to host an ASP.NET MVC app within the same web (i.e. within the same IIS process - same session, modules, etc.)

This works pretty easily as long as the folder/files from both are located in the root folder.

How can I cleanly allow the MVC files to be all contained within a subfolder?

I have a view engine that adds the location formats for the sub-folder. This allows all the controller/view stuff to work fine, but I need a good solution for content files. Currently, I have to be careful to ensure that all the paths in the MVC app to point to the MVC subfolder name, as in:

Url.Content("~/mvc/Content/Site.css")

What are some solid options for accomplishing this?

I don't want any requests going to or from Web Forms to be affected. This logic should only manipulate URLs that can be resolved within the MVC engine (or manipulate all URLs except URLs that can be resolved by the Web Forms engine alone)

Edit: It is a customer requirement that both sites share the same IIS Session, so a separate application is off the table for now. Not interested in sharing session between IIS processes at this point.

8
  • Can you create a sub application within the IIS site? (IIS Site > Add Application) Commented Apr 16, 2012 at 17:15
  • Why dont the standard folders work? MVC views and controllers typically go into a ~/Views/ folder, and any supporting files such as CSS and JS can be placed in other folders and referenced as appropriate. Commented Apr 16, 2012 at 17:15
  • I want to keep the site's MVC-related folders/files separate because this project involves a long migration path at the end of which the web-forms site will go away entirely. This question is part of an attempt to limit the changes to the existing site as well as limit the restructure/organization necessary to decommission the web forms site. Commented Apr 16, 2012 at 17:18
  • @jorgebg, it is a customer requirement that both sites share the same IIS Session, so a separate application makes this more difficult to implement/maintain reliably. Commented Apr 16, 2012 at 17:20
  • @heiserman: You can integrate the MVC project inside a WebForms project hanselman.com/blog/… I'm doing the same in some projects and works great as you can reuse some resources (CSS, images) Commented Apr 16, 2012 at 17:32

2 Answers 2

3

I would just make a set of URL helper extensions that would take this into account for me.

The Extension Class

namespace Core.Extensions{
   public static class UrlHelperExtensions
   {
      public static string Image(this UrlHelper helper, string fileName)
      {
        return helper.Content("~/mvc/Content/Images/" + fileName);
      }

      public static string Stylesheet(this UrlHelper helper, string fileName)
      {
        return helper.Content("~/mvc/Content/Css/" + fileName);
      }

      public static string Script(this UrlHelper helper, string fileName)
      {
        return helper.Content("~/mvc/Content/Scripts/" + fileName);
      }
   }
}

Web.Config

<system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="System.Web.Mvc.WebViewPage">
      <namespaces>
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="Core.Extensions" />  <-- Add your extension namespace here
      </namespaces>
    </pages>
  </system.web.webPages.razor>

Usage in Views

<link href="@Url.Stylesheet("site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Script("jquery-1.7.1.min.js")" type="text/javascript"></script>
<img src="@Url.Image("MyImageName.jpg")" />

<!--CSS in a sub directory from your root that you specified in your Extension class -->
<link href="@Url.Stylesheet("SomeDirectory/otherCss.css")" rel="stylesheet" type="text/css"/>
Sign up to request clarification or add additional context in comments.

Comments

1

One workable solution is to create a sub-domain for the MVC application, ex. mvc.mydomain.com. It provides a clear separation between the applications that are easily integrated, and does not require an additional domain.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.