Question

I have peice of code in ASP.Net for execute code of C# using roslyn compiler. However, I use ASP.Net web forms to build user interface for execute the code. So,I want to take code from one textbox and by "Compile" button I show the result in another textbox.

So, I have this controls:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>

<asp:Content runat="server" ID="FeaturedContent" ContentPlaceHolderID="FeaturedContent">
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>Compiler Tool</h1>
            </hgroup>
            <p>
                You can compile your source code of C# using Roslyn Compiler from Microsoft</p>
            <br/>

            <p>
                <asp:TextBox ID="TextBox3" runat="server" Height="185px"  Width="480px" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>
            </p>
            <p>

                <asp:Button ID="Button2" runat="server" onclick="Button_Click" Text="Compile" ForeColor="Black" />
            </p>
            <p>
                <asp:TextBox ID="TextBox2" runat="server" Height="138px" Width="390px" ></asp:TextBox>
            </p>
        </div>
    </section>
</asp:Content>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    </asp:Content>

And here the Handler event for it using C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using Roslyn.Scripting;
using Roslyn.Scripting.CSharp;


namespace WebApplication2
{
    public partial class _Default : Page
    {



        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Click(object sender, EventArgs e)
        {

            var engine = new ScriptEngine();
            var session = engine.CreateSession();
            String code = TextBox3.Text;
            session.Execute(code);

        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

How could I show the result of execution of Code (TextBox3) in TextBox2??

Iam beginner in ASP.Net environment, Any help??

Was it helpful?

Solution

I'm afraid I'm not near a dev pc, so I can't test the code, but here's what comes to mind:

You're not using the result of session.Execute(code);, so if a user were to put in 3+3, it would get executed, and the return value would be 6, but since it's not captured, it simply would go out of scope. If instead, you did object result = session.Execute(code);, the output would be the integer 6; You could then call .ToString() on result and put that in your text box.

And of course be careful allowing users to execute arbitrary code on your webserver...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top