ASP.NET MVC路由和路径是JS文件 [英] ASP.NET MVC routing and paths is js files

查看:1185
本文介绍了ASP.NET MVC路由和路径是JS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们现在面临的一个问题。我们有一个loooong内嵌脚本,做所有的工作pretty大页面。我们现在想混淆和使用它作为一个单独的.js文件。但问题是,我们这是由URL助手生成的路径(Url.Content())。那么,什么是分离的js从页面文件,而不是使用硬codeD路径字符串的最佳方式?

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?

推荐答案

我通常写我的不同的视图的JavaScript(只有JS code)以及用我自己的行动结果来呈现它。这样我可以利用C#在服务器端,如果需要,我可以用一个模型,它会被包括在浏览器中的外部JS文件(在适当的高速缓存)。我用的是动作的结果可以在这里找到: HTTP://$c$cpaste.net/p2s3po

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

更新

您可以使用操作的结果是这样,从你的行动:

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

然后你只需把它当作好像它是一个普通视图(但只能在视图中写javascript)。你可以把任意数量的参数以及课程。

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>

这篇关于ASP.NET MVC路由和路径是JS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆