web-dev-qa-db-ja.com

ASPNET MVC 3とRazorを使用した個別のJavaScriptファイルの@ Url.Content

これを使っていた

if (ret = 1)
    iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image.png")');
else if (ret = 2)
    iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image2.png")');
else if (ret = 3)
    iconType = new google.maps.MarkerImage('@Url.Content("~/Content/images/image3.png")');

ビュー(ASPNET MVC 3)では、コードを別のJavaScriptファイルに移動します(コントロールの画像として設定した可変値に応じて、image.png、image2.png、またはimage3を使用しているため、これを使用しています。 png)。

Razorはjavascriptファイル内の@ Url.Contentを解析しません。これを処理する最良の方法は何ですか?

前もって感謝します!ギジェルモ。

25
polonskyg

私は通常、ページの最初に次のようなブロックを配置します。

<script>
    var ROOT = '@Url.Content("~")';
</script>

次に、JavaScriptでROOT変数を参照します。

if (ret = 1)
    iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image.png');
else if (ret = 2)
    iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image2.png');
else if (ret = 3)
    iconType = new google.maps.MarkerImage(ROOT + '/Content/images/image3.png")');
47
AHM

別の解決策は、RazorViewEngineを介してJSファイルを完全にレンダリングすることです。このようにして、JavaScriptファイル内でRazor構文を簡単に使用できます。

public class CustomRazorViewEngine : BuildManagerViewEngine
{
    internal static readonly string ViewStartFileName = "_ViewStart";

    public CustomRazorViewEngine()
        : this(null)
    {
    }

    public CustomRazorViewEngine(IViewPageActivator viewPageActivator)
        : base(viewPageActivator)
    {
        AreaViewLocationFormats = new[]
                                    {
                                        "~/Areas/{2}/Views/{1}/{0}.cshtml",
                                        "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                                        "~/Areas/{2}/Views/{1}/{0}.csjs",
                                        "~/Areas/{2}/Views/Shared/{0}.cshtml",
                                        "~/Areas/{2}/Views/Shared/{0}.vbhtml",
                                        "~/Areas/{2}/Views/Shared/{0}.csjs"
                                    };
        AreaMasterLocationFormats = new[]
                                        {
                                            "~/Areas/{2}/Views/{1}/{0}.cshtml",
                                            "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                                            "~/Areas/{2}/Views/{1}/{0}.csjs",
                                            "~/Areas/{2}/Views/Shared/{0}.cshtml",
                                            "~/Areas/{2}/Views/Shared/{0}.vbhtml",
                                            "~/Areas/{2}/Views/Shared/{0}.csjs"
                                        };
        AreaPartialViewLocationFormats = new[]
                                            {
                                                "~/Areas/{2}/Views/{1}/{0}.cshtml",
                                                "~/Areas/{2}/Views/{1}/{0}.vbhtml",
                                                "~/Areas/{2}/Views/{1}/{0}.csjs",
                                                "~/Areas/{2}/Views/Shared/{0}.cshtml",
                                                "~/Areas/{2}/Views/Shared/{0}.vbhtml",
                                                "~/Areas/{2}/Views/Shared/{0}.csjs"
                                            };

        ViewLocationFormats = new[]
                                {
                                    "~/Views/{1}/{0}.cshtml",
                                    "~/Views/{1}/{0}.vbhtml",
                                    "~/Views/{1}/{0}.csjs",
                                    "~/Views/Shared/{0}.cshtml",
                                    "~/Views/Shared/{0}.vbhtml",
                                    "~/Views/Shared/{0}.csjs"
                                };
        MasterLocationFormats = new[]
                                    {
                                        "~/Views/{1}/{0}.cshtml",
                                        "~/Views/{1}/{0}.vbhtml",
                                        "~/Views/{1}/{0}.csjs",
                                        "~/Views/Shared/{0}.cshtml",
                                        "~/Views/Shared/{0}.vbhtml",
                                        "~/Views/Shared/{0}.csjs"
                                    };
        PartialViewLocationFormats = new[]
                                        {
                                            "~/Views/{1}/{0}.cshtml",
                                            "~/Views/{1}/{0}.vbhtml",
                                            "~/Views/{1}/{0}.csjs",
                                            "~/Views/Shared/{0}.cshtml",
                                            "~/Views/Shared/{0}.vbhtml",
                                            "~/Views/Shared/{0}.csjs"
                                        };

        FileExtensions = new[]
                            {
                                "cshtml",
                                "vbhtml",
                                "csjs",
                            };
    }

    protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
    {
        return new RazorView(controllerContext, partialPath,
                             layoutPath: null, runViewStartPages: false, viewStartFileExtensions: FileExtensions,
                             viewPageActivator: ViewPageActivator);
    }

    protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
    {
        var view = new RazorView(controllerContext, viewPath,
                                 layoutPath: masterPath, runViewStartPages: true, viewStartFileExtensions: FileExtensions,
                                 viewPageActivator: ViewPageActivator);
        return view;
    }
}

あなたのグローバルasaxファイルにこれらを追加してください:

RazorCodeLanguage.Languages.Add("csjs", new CSharpRazorCodeLanguage());
ViewEngines.Engines.Add(new CustomRazorViewEngine());

そして、このマッピングをweb.configルートに追加します

   <system.web>
          <compilation debug="true" targetFramework="4.0">
                 <assemblies>
                       <add Assembly="Newtonsoft.Json" />
                       <add Assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                       <add Assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                       <add Assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                       <add Assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                       <add Assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
                 </assemblies>

                 <!--Added-->
                 <buildProviders>
                       <add extension=".csjs" type="System.Web.WebPages.Razor.RazorBuildProvider, System.Web.WebPages.Razor"/>
                 </buildProviders>

JavaScriptファイル内でモデルを操作することもできます!

public ActionResult MyJavascriptThroughRazor()
{
    var someModel = ...
    return PartialView("filenamewithcsjsextension",someModel);
}
8
Beriz