admin管理员组文章数量:1332352
we're facing a problem now. We have a pretty big page with a loooong inline script, which does all the work. We now want to obfuscate it and use it as a separate .js file. But the problem is that we have paths which are generated by Url helper (Url.Content()). So what is the best way to separate js file from the page and not using hard-coded path strings?
we're facing a problem now. We have a pretty big page with a loooong inline script, which does all the work. We now want to obfuscate it and use it as a separate .js file. But the problem is that we have paths which are generated by Url helper (Url.Content()). So what is the best way to separate js file from the page and not using hard-coded path strings?
Share Improve this question asked Jun 2, 2010 at 12:13 HiveHicksHiveHicks 2,3345 gold badges29 silver badges42 bronze badges2 Answers
Reset to default 9I usually write my javascript in separate views (with only js code) and use my own action result to render it. This way I can take advantage of c# on the server side and I can use a model if needed and it will be included as a external js file in the browser (with appropriate caching). The action result I use can be found here: http://codepaste/p2s3po
Update
You can use the action result like this from your action:
public ActionResult JsFile() {
ViewData.Model = //Create model if you want one;
return new JavascriptFileResult(true)
{
TempData = TempData,
ViewData = ViewData
};
}
Then you just treat it as if it was a normal view (but only write javascript in the view). You can take any number of parameters as well of course.
You can include it like this:
<script type="text/javascript" src="<%=Url.Action("JsFile", "ControllerName")%>"></script>
You could create javascript functions to set the required paths and invoke them from a small script section from the page.
Javascript file:
var resource1;
var resource2;
function setResourcesReferences(resource1, resource2, ...) {
}
ASPX file:
<script type="text/javascript">
setResourcesReferences(<% Url.Content("Resource1") %>, <% Url.Content("Resource2") %>, ...);
</script>
本文标签: javascriptASPNET MVC routing and paths is js filesStack Overflow
版权声明:本文标题:javascript - ASP.NET MVC routing and paths is js files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322303a2453033.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论