Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
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:
XML
<%@ 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#:
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??
Posted
Updated 22-Mar-14 7:09am
v2

1 solution

Hi,

If you are a beginner in asp.net I wonder what you are trying to achieve by this.

If you are trying to build a tool where you would paste code snippets and see if they work - something like a quick test - there are tools in the market already for this. Such as

linqpad/[^]
dotnetfiddle/[^]
csharppad[^]
Snippet Compiler/[^]

And a host of others listed here[^]

However, if you are still interested in going ahead with what you are planning, then please be prepared to be overwhelmed a bit and go through this article[^], pretty comprehensive.

[Please accept/up-vote answers or solutions that work for you to encourage participation]
 
Share this answer
 
Comments
Y.Mohammed 23-Mar-14 2:46am    
Actually, I need to build that simple tool to support my research to show it as a prototype in the web application. I was done it as console application and it works but now I have to build user interface which accept the code from user and present the result in simple way.
So, If you know the way of doing that guide me.
Thanx CoderPanda.
CoderPanda 23-Mar-14 2:57am    
Okay, do you see any associated ToString() method on the session or engine variable in your code? Or do you see the return type of the execute method? I think it is just a matter of getting this result and showing in your TextBox2.
Y.Mohammed 23-Mar-14 5:07am    
Exactly that was the gap here.
I will try to search for it more
CoderPanda 23-Mar-14 5:43am    
Upvoted your question. I don't get to see interesting questions like these too often.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900