我有一个 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/