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?
I 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.net/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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With