C
C#•9mo ago
IcyIme

I want to make a dynamic compiler of c# code and with that have a console on the web page, using thi

I want to make a dynamic compiler of c# code and with that have a console on the web page, using this it will be possible to interact with the code
8 Replies
IcyIme
IcyImeOP•9mo ago
but i do not know how and where to startt
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Threading.Tasks;

namespace DynamicScriptingAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ScriptController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> ExecuteScript([FromBody] ScriptRequest request)
{
try
{
// Compile the code
Script<object> script = CSharpScript.Create(request.Code)
.WithOptions(ScriptOptions.Default
.WithReferences(typeof(Console).Assembly));

// Execute the code
var result = await script.RunAsync();

// Return the result
return Ok(result.ReturnValue);
}
catch (CompilationErrorException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}

public class ScriptRequest
{
public string Code { get; set; }
}
}
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using System;
using System.Threading.Tasks;

namespace DynamicScriptingAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class ScriptController : ControllerBase
{
[HttpPost]
public async Task<IActionResult> ExecuteScript([FromBody] ScriptRequest request)
{
try
{
// Compile the code
Script<object> script = CSharpScript.Create(request.Code)
.WithOptions(ScriptOptions.Default
.WithReferences(typeof(Console).Assembly));

// Execute the code
var result = await script.RunAsync();

// Return the result
return Ok(result.ReturnValue);
}
catch (CompilationErrorException ex)
{
return BadRequest(ex.Message);
}
catch (Exception ex)
{
return StatusCode(500, ex.Message);
}
}
}

public class ScriptRequest
{
public string Code { get; set; }
}
}
Pobiega
Pobiega•9mo ago
You might want to look at the source code for modix and the code execution bit. I don't have the links right now thou
Pobiega
Pobiega•9mo ago
GitHub
GitHub - discord-csharp/MODiX: Discord Bot handling basic moderatio...
Discord Bot handling basic moderation needs, soon implements statistics. - discord-csharp/MODiX
Pobiega
Pobiega•9mo ago
GitHub
GitHub - discord-csharp/CSharpRepl: CSharpRepl is a web api for C# ...
CSharpRepl is a web api for C# REPL using the Roslyn Scripting APIs - discord-csharp/CSharpRepl
Pobiega
Pobiega•9mo ago
Found it
IcyIme
IcyImeOP•9mo ago
i do not know how to use it 😦
Pobiega
Pobiega•9mo ago
You are asking for something incredibly dangerous and non-trivial to implement. So.. do some research These are not "just use this and all works fine" solutions
Unknown User
Unknown User•9mo ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server