Is there an easy way to render absolute URLs with Microsoft Web Optimization framework / script bundling?

后端 未结 1 1413
轻奢々
轻奢々 2021-01-04 18:38

I\'m trying to render a JavaScript bundle using Microsoft\'s Web Optimization framework, like this:

@Scripts.Render(\"~/assets/bundle.js\")

相关标签:
1条回答
  • 2021-01-04 18:47

    One easy way is with Scripts.RenderFormat:

    @Scripts.RenderFormat("<script src='http://my.site.com{0}'></script>","~/assets/bundle.js")
    

    A way to get URL from request. Couldn't seem to use multiple parameters with the RenderFormat, so that's why it looks a little ugly:

     @Scripts.RenderFormat("<script src='//" + @Request.Url.Host + "/{0}'></script>", "~/assets/bundle.js")
    

    or better yet, centralize a function to get the correct path (using a fictional function):

    @Scripts.RenderFormat("<script src='" + @Tools.GetRootURL() + "{0}'></script>", "~/assets/bundle.js")
    

    Also, you don't need the .js on the bundle:

    bundles.Add(new ScriptBundle("~/assets/bundle")
    
    0 讨论(0)
提交回复
热议问题