@Url.Content in separate javascript file using ASPNET MVC 3 and Razor

前端 未结 2 859
独厮守ぢ
独厮守ぢ 2020-12-08 03:26

I was using this

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


        
相关标签:
2条回答
  • 2020-12-08 03:32

    Another solution is rendering your JS files completely through the RazorViewEngine. This way you could easily use the Razor Syntax within your Javascript file:

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

    In Your Global asax file just add these:

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

    And add this mapping in the web.config root

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

    You can now even work with a Model inside your javascript file!

    public ActionResult MyJavascriptThroughRazor()
    {
        var someModel = ...
        return PartialView("filenamewithcsjsextension",someModel);
    }
    
    0 讨论(0)
  • 2020-12-08 03:49

    I usually put a block like this in the beginning of the page:

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

    And then i refer to the ROOT variable in javascript:

    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")');
    
    0 讨论(0)
提交回复
热议问题