[英]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.