Hosting Dotnet.Script in Asp.net to execute CSX files
I am in the process of migrating a large .Net 4.8 web application to .Net Core 3.1. We use a CSX file to build our configuration dictionary and execute it using ScriptCS. ScriptCS doesn’t support .Net Core but luckily Dotnet Script does. I looked around online for some examples of how to execute a CSX file using Dotnet Script from within an Asp.net Core web application but could find any. I was able to throw together a working example, but of course no guarantees for production-readiness.
What got me on the right track was a Github issue that mentioned using ScriptRunner.Execute (link below).
To get that example to work was to add the `Dotnet.Script.Core` Nuget package to the web project. The code below will then allow you to execute a CSX file at a given path, passing any arguments, and return the result.
The only change I had to make to the CSX script itself was to use Args
instead of Env.ScriptArgs
to access the arguments passed to the script, and use return result;
instead of just result
(with no return).