これを考慮した一連の 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>
ビューでの使用
<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"/>