3

我有一个 ASP.NET 4.0 Web 表单应用程序,需要在同一个 Web 中托管一个 ASP.NET MVC 应用程序(即在同一个 IIS 进程中 - 相同的会话、模块等)

只要两者中的文件夹/文件都位于根文件夹中,这很容易工作。

我怎样才能干净地允许 MVC 文件全部包含在子文件夹中?

我有一个视图引擎,可以为子文件夹添加位置格式。这允许所有控制器/视图的东西正常工作,但我需要一个好的内容文件解决方案。目前,我必须小心确保 MVC 应用程序中的所有路径都指向 MVC 子文件夹名称,如下所示:

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

有哪些可靠的选择可以实现这一目标?

我不希望任何进出 Web 表单的请求受到影响。此逻辑应仅操作可在 MVC 引擎内解析的 URL(或操作除可单独由 Web 窗体引擎解析的URL之外的所有 URL)

编辑: 客户要求两个站点共享相同的 IIS 会话,因此暂时不考虑单独的应用程序。此时对在 IIS 进程之间共享会话不感兴趣。

4

2 回答 2

3

我只会制作一组 URL 帮助程序扩展,这些扩展会为我考虑到这一点。

扩展类

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);
      }
   }
}

网络配置

<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>

视图中的用法

<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"/>
于 2012-04-16T18:08:16.457 回答
1

一种可行的解决方案是为 MVC 应用程序创建一个子域,例如。mvc.mydomain.com。它在易于集成的应用程序之间提供了清晰的分离,并且不需要额外的域。

于 2012-04-16T17:25:46.327 回答