I want to be able to create a ScriptBundle from a source that is not a *.js file but rather from the contents of a string variable
My problem
I have a series of enums and constants from different classes in my source code.
I would like an equivalent of these enumerations and constants for use with javascript.
Currently 2 methods are available to me:
- Manually write the equivalences in a *.js file then include it in the ScriptBundle
- Do not use a *js file but use a method for dynamically creating these enumerations and constants from *.cshtml views
Method 1 is tedious and can generate oversights if the enumerations are changed.
Method 2 requires systematic regeneration at each page refresh.
Is there a solution to add the content of a variable to the ScriptBundle? Something that would look like this:
string myString = @"
const Direction = Object.freeze({
North: 'north',
East: 'east',
West: 'west',
South: 'south',
})
";
private static string GenerateJsEnum()
{
string text = string.Empty;
// convert enums
return text;
}
bundles.Add(new ScriptBundle("~/bundles/MyScript").IncludeString(myString, GenerateJsEnum());
Thank you for your answers