簡體   English   中英

從獨立的.aspx頁調用服務器端類

[英]Call server side class from standalone .aspx page

我有一個生成獨立ASPX頁面的應用程序,這些頁面在C#中具有自己的script

現在,我不想將所有的c#腳本代碼添加到c# script標簽中,所以我想調用一個后端c#類,該類將包含所有腳本代碼(這是正常的c#代碼)。 我想從此腳本中調用該后端c#類

<script language="CS" runat="server"> 
MyClass myclass = new MyClass();// backend class
myclass.GetAllScripts(); //say this is the fucntion which contains scripting 
code
</script>

您可以將生成的代碼保存在App_Code文件夾中,此文件夾中的代碼將在運行時進行編譯,並准備好用於應用程序的其他部分

例如:

var generatedCode = 
@"
    namespace MyProject
    {
        public class MyClass
        {
            public void GetAllScripts()
            {
                // ...
            }
        }
    }
";
var generatedPage = 
@"
    <%@ Page Language=""C#"" AutoEventWireup=""true"" %>
    <html>
    <head>
        <title>Test</title>
        <script language=""CS"" runat=""server"" >
            void Page_Load(object sender, EventArgs e)
            {
                //below code will be executed when the page is opened
                MyClass myclass = new MyClass();// backend class
                myclass.GetAllScripts();
            }
        </script>
    </head>
    <body>
        ...
    </body>
    </html>
";

// change to the path and file name to fit your need, but the cs file must in ~/App_Code
var aspxPath = Path.Combine(Server.MapPath("~"), "GeneratedPage.aspx");
System.IO.File.WriteAllText(aspxPath, generatedPage);

var csPath = Path.Combine(Server.MapPath("~/App_Code"), "MyClass.cs");
System.IO.File.WriteAllText(csPath, generatedCode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM