asp.net - 需要一种在 ASP.NET Web 窗体应用程序的子文件夹中托管 ASP.NET MVC 文件的好方法

标签 asp.net asp.net-mvc webforms

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

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

我怎样才能让 MVC 文件完全包含在一个子文件夹中?

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

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

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

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

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

最佳答案

我会制作一组 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);
      }
   }
}

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>

在 View 中的使用

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

关于asp.net - 需要一种在 ASP.NET Web 窗体应用程序的子文件夹中托管 ASP.NET MVC 文件的好方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10178579/

相关文章:

c# - 获取静态对象上的锁会阻塞其他请求线程吗?

Jquery 对话框和下拉框

c# - 更改 Html.TextBox 的大小

c# - 下载链接失效

asp.net - ASP :LinkButton and Eval

asp.net-mvc - 指定的包含路径无效。 EntityType 未声明具有名称的导航属性

c# - 从代码更改 CSS 类

jquery - 如何隐藏空嵌套中继器?

asp.net-mvc - MVC 中 "Master Page"逻辑应该放在哪里?

c# - EnableViewState 会影响 GridView 上的哪些控件?