Need a good approach for placing ASP.NET MVC files in a subfolder in an ASP.NET Web Forms application

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

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

How can I completely allow MVC files to be contained inside a subfolder?

I have a view mechanism that adds location formats for a subfolder. This allows all the controller / view functions to work fine, but I need a good solution for the content files. Currently, I have to be careful that all paths in the MVC application point to the name of the MVC subfolder, for example:

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

What are some reliable options for this?

I do not want any requests to come in or from web forms to be affected. This logic should only process URLs that can be resolved in the MVC engine (or manage all URLs except URLs that can only be resolved by the Web Forms engine)

Change: . The client’s requirement is that both sites use the same IIS session, so a separate application is currently disabled. Not interested in a collaborative session between IIS processes at this time.

+3
source share
2 answers

I would just create a set of helper urls that take this into account for me.

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"/>
+3
source

- MVC, . mvc.mydomain.com. , .

+1

All Articles