简体   繁体   中英

Ouput Javascript from C# (ASP.NET 3.5 VS2008)

I'm trying to output Javascript from a C# ASP.NET MVC 2 website.

Basically, I'm creating a widget that people can add their website (by just adding a script tag which gets its code from an MVC action on my website.)

The reason for this is I want the users to be able to configure the output of the script via my website.

(colours of injected DOM elements, position of the widget on screen [Top left, Bottom right etc])

I've looked at script# but it looks a bit overkill. I've started using StringBuilders and outputing Javascript but its very messy and I don't think will be maintainable.

Any ideas?

Many thanks

Sam

The way I approached this was to make template scripts (text files) that are embedded resources in the assembly. Then it's a simple matter of pulling out the resource and replacing tagged fields in the templates with the relevant values. Works quite nicely and the template files are easy to edit, but you need to validate and/or escape the values going into the template if they are supplied by the client in order to avoid breaking the generated JS (and potentially introducing a security hazard). There's no reason why they couldn't be loaded from disk, and it's probably an excellent candidate to go into the HttpRuntime cache.

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