简体   繁体   English

如何在ASP.Net Web表单中使用Roslyn执行C#代码?

[英]How I can execute C# code using Roslyn in ASP.Net web form?

I have peice of code in ASP.Net for execute code of C# using roslyn compiler. 我在ASP.Net中有足够的代码来使用roslyn编译器执行C#的代码。 However, I use ASP.Net web forms to build user interface for execute the code. 但是,我使用ASP.Net Web表单来构建用于执行代码的用户界面。 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#: 这里是使用C#的Handler事件:

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?? 如何显示TextBox2中代码(TextBox3)的执行结果?

Iam beginner in ASP.Net environment, Any help?? Iam在ASP.Net环境中的初学者,有帮助吗?

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); 您没有使用session.Execute(code);的结果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. ,因此,如果用户输入3+3 ,它将被执行,返回值将为6,但由于未捕获,因此将超出范围。 If instead, you did object result = session.Execute(code); 如果相反,则您的object result = session.Execute(code); , the output would be the integer 6; ,输出将为整数6; You could then call .ToString() on result and put that in your text box. 然后,您可以在result上调用.ToString()并将其放在文本框中。

And of course be careful allowing users to execute arbitrary code on your webserver... 当然要小心,允许用户在您的Web服务器上执行任意代码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过单击表单上的ac#asp.net按钮,如何在继续执行下一行代码之前执行一行代码? - By the click of a c# asp.net button on a form, how can I execute a line of code before moving on to the next lines of code? 如何在 Asp .Net 网站中执行 C# 代码 - How can i execute C# code in Asp .Net Website 我如何使用asp.net C#在Web应用程序中显示捕获的指纹 - How can i show captured fingerprint in web application using asp.net C# 如何在同一个ASP.NET Web窗体应用程序中使用c#和vb.net代码? - How can I have c# and vb.net code behinds in the same ASP.NET Web Forms application? 如何在ASP.NET MVC 5应用程序中使用Roslyn? - How Can I Use Roslyn in an ASP.NET MVC 5 App? 如何从C#ASP.NET Web表单背后的代码获取图表值 - How to get values of chart from code behind c# asp.net web form 如何覆盖C#asp.net Web表单的渲染? - How to override render for C# asp.net web form? 如何使用C#在ASP.NET中的JavaScript函数中执行 - How to execute in JavaScript function in ASP.NET using C# ASP.NET C#:如何在 web.config 中调用 App_code 函数 - ASP.NET C#: How can I call App_code functions inside of web.config 如何使用 c# 在 asp.net web 窗体中使用 WebSocket [TCP 连接] - How to work with WebSocket[TCP connection] in asp.net web form using c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM