我想在 ASP.Net MVC 的服务器端生成一些 JavaScript。 有支持这个的视图引擎吗? 理想情况下,我希望能够从这样的 url 获取 JavaScript:
http://myapp/controller/action.js
我查看了 MonoRail 项目,他们似乎有此功能,但它非常缺乏文档,而且我找不到任何 ASP 的端口。网络 MVC。
编辑: 这个想法是能够通过使用像这样的 url 将页面呈现为标准 HTML,
http://myapp/controller/action
并通过使用问题中的第一个 url 呈现为 js(特别是 ExtJS 组件)。 控制器中只有一个操作,但有两个视图:一个用于 HTML,一个用于 JS。
编辑2:我基本上想达到与路由器扩展解析/请求处理。
I would like to generate some JavaScript on the server side in ASP.Net MVC. Is there a view engine that supports this? Ideally I would like to be able to get JavaScript from an url like:
http://myapp/controller/action.js
I've looked at the MonoRail project, and they seem to have this feature, but it's very lacking in documentation, and I can't find any ports to ASP.Net MVC.
Edit: The idea is to be able to render a page both as standard HTML by using a url like:
http://myapp/controller/action
and as js (specifically an ExtJS component) by using the first url in the question. There would be only a single action in the controller, but two views: one for HTML and one for JS.
Edit 2: I basically wanted to achieve the same result as router extension parsing/request handling in CakePHP.
发布评论
评论(6)
我想扩展这个想法,不仅允许 Javascript 视图,而且或多或少允许任何类型的文档。 要使用它,您只需将 *.js url 的视图放入控制器视图文件夹的子文件夹中:
该类是任何类型的 ViewEngine 的装饰器,因此您可以将它与 NVelocity/WebForms/Whatever 一起使用:
然后,在您的Global.asax.cs 文件:
感谢大家的帮助!
I wanted to extend this idea to not only allow Javascript views, but more or less any type of document. To use it, you just put the views for *.js urls in a subfolder of your controller's view folder:
The class is a decorator for any type of ViewEngine, so you can use it with NVelocity/WebForms/Whatever:
Then, in your Global.asax.cs file:
Thanks for everyone's help with this!
根据您的编辑,我将尝试使用新答案,假设您需要 ExtJS 的 json 数据。 我刚刚在我正在构建的应用程序中对其进行了测试,并且运行良好。 首先你需要两个路由
现在 Controller 类有一个 Json 方法来序列化你想要的任何对象,它是一个 JsonResult 所以你可以使用:
Based on your edit I'll try with a new answer asumming you need json data for ExtJS. I've just tested it in the app I'm building and it works fine. First you need two routes
Now the Controller class has a Json method to serialize whatever object you want and it's a JsonResult so you can just use:
2007 年 7 月的 ASP.NET Futures 版本包含新的托管 JScript(我找不到更新的)。 我已经成功地尝试了一些 - 但请注意,您可能会被迫做一些额外的工作,JScript 的 .ASPX 解析存在无法使用的错误。
http://www.microsoft.com/downloads/details.aspx microsoft.com/downloads/details.aspx?FamilyId=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en
The July 2007 ASP.NET Futures release has the new Managed JScript (I can't find a newer one). I've successfully batted it around a bit - but beware, you will probably be forced to do some extra work, the .ASPX parsing for JScript is unusably buggy.
http://www.microsoft.com/downloads/details.aspx?FamilyId=A5189BCB-EF81-4C12-9733-E294D13A58E6&displaylang=en
您不一定需要视图引擎,只需返回纯文本结果即可。
这是一个控制器(MonoRail),我用来在 javascript 对象中渲染一些用户文化设置:
对于原始文本渲染会产生气味的更复杂的事情,任何视图引擎都可以工作。
此外,您不必使用 .js 扩展名将 url 放入脚本标记中。
You don't necessarily need view engine for that, just return plain text result.
Here is a controller (MonoRail) I've used to render some user culture settings in a javascript object:
for more complex things for which raw text rendering would smell, any view engine would work.
Also you are not forced to use .js extension to put the url in your script tag.
我必须做一些非常类似的事情,并且我使用了 MVCContrib 的 nvelocity 视图引擎 - 从技术上讲,你可以使用默认的 aspx 视图引擎,但我发现 nvelocity 语法对于输出脚本来说要简单得多(如果您以前没有使用过它,请不要担心 - 我花了大约 10 分钟来弄清楚它!)。
然后,您只需向路由表添加一条路由即可将您的 .js url 定向到操作!
编辑
无法验证这一点,因为我手头没有视觉工作室,但对于路线,您可能会遇到这样的情况:
以 .js 结尾的请求应该经过第一个路线 - 无扩展名请求下降到第二个。
然后你的操作可以有一个 requestType 参数:
至于目录结构 - 你就得靠你自己了! 不是因为我不想提供帮助,而是因为您可以灵活地用它做您想做的事情!
I had to do something very similar, and I used the nvelocity view engine from MVCContrib - technically you could use the default aspx view engine, but I found that the nvelocity syntax was a lot more straightforward for puting out script (don't worry if you haven't used it before - I spent about 10 minutes figuring it out!).
Then you just need to add a route to the route table to handle directing your .js url to the action!
EDIT
Can't verify this as I don't have visual studio to hand but for the route, you might have something like this:
Requests ending in .js should go through the first route - extensionless requests fall through to the second.
Then your action could have a requestType param:
As for directory structure - you're on your own with that! Not because I don't want to be helpful, but more down to the fact that you have flexibility to do what you want with it!
如果您只想基于 ViewData 生成 JavaScript,您可以创建自己的自定义结果。 这里是一个示例。
If you just want to generate a javascript based on the ViewData you can create your own custom result. Here is an example.