有人使用 ASP.net (webforms) 动态生成 javascript 和/或 css 文件吗?
我更喜欢使用外部 css 和 javascript 文件。 然而,在很多情况下,javascript 或 css 文件的内容需要是动态的。 我通常只是将 javascript 或 css 传输到我的 aspx 页面中的内联或内页代码,并在那里处理动态内容。
有人有更好的方法吗? 有没有办法使用 ASP.NET 的常规模板语言生成整个 js 或 css 文件?
我目前正在使用 Webforms,但我也有兴趣在 MVC 中解决这个问题。
谢谢
I prefer the use of external css and javascript files. There are however many cases where the content of a javascript or css file needs to be dynamic. I'll usually just transfer the javascript or css to inline or inpage code in my aspx page and handle the dynamic stuff there.
Does anyone have a better approach? Would there be a way to generate entire js or css files using asp.net's regular templating language?
I'm currently using webforms but I'd be interested in solving this issue in MVC as well.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我使用 HTTPHandler 发回动态之前的JavaScript。 但不是从 System.Web.UI.Page 继承的东西。
使用 HTTPHandler 和 ASHX 或 AXD 是发回资源的“ASP.Net”方式动态地。
I've used a HTTPHandler to send back dynamic javascript before. But not something that inherits from System.Web.UI.Page.
Using a HTTPHandler and an ASHX or AXD is the "ASP.Net" way to send back resources dynamically.
我已经使用了动态 CSS 的处理程序。 根据您的需要,您可以对 js 文件执行相同的操作。
我有一个 css 文件,其中包含需要动态的部分(如 ##bacgroundcolor##)的占位符,并且处理程序只是根据需要进行替换。
我还使用了一种方法,使用 css 类来标记需要特殊行为的 html 元素。 然后静态 js 查找此元素并挂钩适当的处理程序。 使用 jquery 肯定会更容易(当时我用常规 js 做到了:()。
I have used handlers for dynamic css. Depending on what you need, you can do the same for js files.
I had a css file with placeholders for the pieces that needed to be dynamic like ##bacgroundcolor##, and the handler just replaced as appropriate.
I have also used an approach where I use css classes to mark html elements that need special behaviors. Then the static js, looks for this elements and hook the appropriate handlers. This is something that certainly would be even easier with jquery (I did it with regular js back then :().
我以前在 aspx 页面中做过此操作,但在我看来,WebForm 样式本身不太适合渲染严格的 javascript 或 CSS。 每次我完成此操作后,页面最终看起来都非常像经典的 ASP。
I've done this in an aspx page before, but in my opinion the WebForm style doesn't suit itself well to rendering strictly javascript or CSS. Every time I've done it, the page has ended up looking quite a bit like classic ASP.
希望您使用的实际 JavaScript 保持静态,您只需将参数传递给 JavaScript 方法。
hopefully the actual JavaScript you are using would stay static and you would just pass parameters to the JavaScript methods.
我采用了页面标记中包含
<%= control.ClientID %>
之类的 JavaScript 代码,并将其替换为静态 JavaScript。 我将代码重构为类,然后将这些可变部分重构为类成员。 该页面创建该类的一个实例,并设置诸如 ClientID 之类的内容。 这些函数可以是静态的。I have taken JavaScript code that had been in the markup of a page and containing things like
<%= control.ClientID %>
and replaced it with static JavaScript. I refactored the code into a class, I then refactored these variable parts into class members. The page creates an instance of the class, with things like ClientID set. The functions can then be static.