简体   繁体   中英

Does anyone use ASP.net (webforms) to dynamically generate javascript and/or css files?

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

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.

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 :().

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方法。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM