Run script from a web url to visual c# RRS feed

  • Question

  • User-1926441402 posted


    Friday, March 29, 2019 2:31 PM

All replies

  • User753101303 posted


    Unclear. Do you mean you have C# code retrieving a web page and you want to run scripts found in this page ? Or what ?

    Or you want to run C# "script" (or rather code) when a url is hit which is just what an ASP.NET does.
    Or you want to run a script injected in a web page from your C# code...

    I find "from a web url to C#" to be quite confusing. Keep in mind we know nothing else than what you tell us. Short questions are fine but they should be long enough so that we don't have to guess about what you are trying to do...

    Friday, March 29, 2019 4:46 PM
  • User-1926441402 posted

    like this code:
    using System.CodeDom.Compiler;
    using Microsoft.CSharp;

    1. var csc = new CSharpCodeProvider();

    //this are dll-s for console application
    var parameters = new CompilerParameters(new[] { "mscorlib.dll", "System.Core.dll" }, programPath,true);
    parameters.GenerateExecutable = true;

    3. CompilerResults result = csc.CompileAssemblyFromSource(parameters, code);


    but that code will be to WindowsFormsApp cuz the dlls in the code is for console app and change the code that will read from a url the code and not from a txt file thats possible?

    Friday, March 29, 2019 5:31 PM
  • User753101303 posted

    Would have never imagined that though I'm not sure what is the big picture. You could use https://docs.microsoft.com/en-us/dotnet/api/system.net.webclient.downloadstring?view=netframework-4.7.2 to get C# code served from some url and compile this code.

    And your web app would then load and use this DLL ?????

    The big picture could help and it could raise security issues. What are you trying to solve this way ? You try to allow 3rd party to run custom code from within your app ????

    Friday, March 29, 2019 5:43 PM
  • User-893317190 posted

    Hi ahron ,

    code  in the method csc.CompileAssemblyFromSource(parameters, code);   seems to be the source  c# code you want to compile.

    If so, if you want to get source code from a url , you could send a webrequest to get the content of the url  and then set your code to the response body you get from the response.

    About how to send a http request , you could use HttpClient, please refer to the link below to see how to use httpclient to send a webrequest  and get response string from it.


    For example , if you get the response which contains source code, you could pass the string to your method.

     using (HttpClient client = new HttpClient())
             HttpResponseMessage response = await client.GetAsync("your url");
             string responseBody = await response.Content.ReadAsStringAsync();
    CompilerResults result = csc.CompileAssemblyFromSource(parameters, responseBody );

    You could also use WebRequest.

    WebRequest request = WebRequest.Create("your url");
                // Get the response.
              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                // Get the stream containing content returned by the server.  
                Stream dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.  
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.  
                string responseFromServer = reader.ReadToEnd();

    Best regards,

    Ackerly Xu 

    Monday, April 1, 2019 4:36 AM