如何将自定义代码添加到在 Umbraco 中创建的文本页面类型项目

How to add custom code to a Text Page type item created in Umbraco

我最近在 Umbraco 7 上安装了 Txt 初学者工具包。在管理页面的内容部分,我在主页下创建了一个文本页面类型的页面。对于页面,我仅限于选择图片和编写内容。在我以前的 ASP.net 网站上,我有以下代码从用户那里获取一些输入并进行成本估算:

<div class="costSection"  runat="server">            
    برآورد قیمت ترجمه:
    <br />
    <asp:Label runat="server" Text="نوع ترجمه "></asp:Label>
    <asp:DropDownList ID="ddlTranslationType" runat="server">
        <asp:ListItem Value="0">فارسی به انگلیسی</asp:ListItem>
        <asp:ListItem Value="1">انگلیسی به فارسی</asp:ListItem>
        <asp:ListItem Value="2">فرانسه به فارسی</asp:ListItem>
    </asp:DropDownList>
    <br />
    <asp:Label runat="server" Text="تعداد کلمه "></asp:Label>
    <asp:TextBox ID="txtWordsCount" runat="server"  CssClass="costSection" ></asp:TextBox><br />
    <asp:Button runat="server" ID="btnCalcCost" CssClass="costSection" OnClick="btnCalcCost_Click" Text="محاسبه هزینه"/>
    <asp:Label Text="" ID="lblCost" runat="server" CssClass="costSection"> </asp:Label>
</div>

而 C# 代码是:

protected void btnCalcCost_Click(object sender, EventArgs e)
{

    int userChoice;
    try{int.TryParse(ddlTranslationType.SelectedValue,out userChoice);}
    catch(Exception ex){
        lblCost.ForeColor = System.Drawing.Color.Red;
        lblCost.Text=ex.Message;
        return;
    }
    int wordsCount;
    if (txtWordsCount.Text == null || !int.TryParse(txtWordsCount.Text,out wordsCount))
    {
        lblCost.ForeColor = System.Drawing.Color.Red;
        lblCost.Text = "لطفا تعداد کلمات را وارد کنید";
        return;
    }
    float cost=0;
    switch (userChoice)
    {
        case 0: //Farsi-English
            cost = (float)wordsCount / 250 * 13000;
            break;
        case 1: //English-Farsi
            cost = (float)wordsCount / 300 * 8000;
            break;
        case 2: //French-Farsi
            cost = (float)wordsCount / 250 * 20000;
            break;            

    }
    long estimatedCost = (long)cost;
    lblCost.ForeColor= System.Drawing.Color.Green;
    lblCost.Text = estimatedCost.ToString()+" تومان ";
}

在我的新 Umbraco post 中,我想要这段代码,但我不知道在哪里添加标记和 C# 代码才能让它工作。

您应该看看 https://our.umbraco.org/DOCUMENTATION/reference/templating/macros/ 上的 Umbraco 宏 - 您可以创建一个局部视图并将您的代码放在那里,然后将宏添加到您的模板或通过编辑器添加到各个页面。