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

旧城冷巷雨未停 提交于 2019-11-30 18:32:58
MikeSmithDev

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")
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!