无法将类添加到aspx.cs文件中

时间:2017-08-17 00:26:06

标签: c# asp.net

我的网站项目中有2个文件,Translator2.aspx和Translator2.aspx.cs。我想在我的Translator2.aspx.cs文件中添加新类(Erwin:Translator2)。但是这样的错误:

  

严重级代码说明项目文件行抑制状态
  错误' translator2_aspx'不包含' Switch'的定义   没有扩展方法' Switch'接受第一个类型的参数   ' translator2_aspx'可以找到(你是否错过了使用指令   或汇编参考?)
  C:\ Users \用户erwin.surya \文档\ Visual   Studio 2017 \ WebSites \ WebSite1 \ Translator2.aspx 33

这是我的Translator2.aspx代码:

<%@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Translator2.aspx.cs" MasterPageFile="~/Site.Master"   Inherits="Translator2" %>

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">


            <div class="form-group">
                    <asp:Label runat="server" AssociatedControlID="English" CssClass="col-md-2 control-label">English</asp:Label>
                    <div class="col-md-10">

                        <asp:TextBox  Height="79px" TextMode="MultiLine" Width="452px"  runat="server" ID="English" CssClass="form-control" />

                    </div>      </div>  



                    <div class="col-md-offset-2 col-md-10">
                        <asp:Button runat="server" OnClick="Submit"  Height="39px" Width="100px" Text="Translate" CssClass="btn btn-default" />
                    </div>

                    <div class="col-md-offset-2 col-md-10">
                        <asp:Button runat="server" OnClick="Switch" Height="39px" Width="100px" Text="Switch" CssClass="btn btn-default" />
                    </div>


<br /><br /><br />
           <div class="form-group">
                    <asp:Label runat="server" AssociatedControlID="Japanese" CssClass="col-md-2 control-label">Japanese</asp:Label>
                    <div class="col-md-10">
                        <asp:TextBox Height="79px" TextMode="MultiLine" Width="452px"   runat="server" ID="Japanese" CssClass="form-control" />

                    </div></div> 
<br /><br />
            <asp:PlaceHolder runat="server" ID="ErrorMessage" Visible="false">
                    <p class="text-danger">
                        <asp:Literal runat="server" ID="FailureText" />
                    </p>
                </asp:PlaceHolder>
</asp:Content>

这是我的Translator2.aspx.cs代码:

     partial class Translator2 : Page

{
    public async void Submit(object sender, EventArgs e)
{



    string key = "92fcf1387f844a8";
    var authTokenSource = new AzureAuthToken(key.Trim());
    string authToken;
    try
    {
        authToken = await authTokenSource.GetAccessTokenAsync();
    }

    catch (HttpRequestException)
    {
        if (authTokenSource.RequestStatusCode == HttpStatusCode.Unauthorized)
        {
            Console.WriteLine("Request to token service is not authorized (401). Check that the Azure subscription key is valid.");
            return;
        }
        if (authTokenSource.RequestStatusCode == HttpStatusCode.Forbidden)
        {
            Console.WriteLine("Request to token service is not authorized (403). For accounts in the free-tier, check that the account quota is not exceeded.");
            return;
        }
        throw;
    }


    string output = "";
    string text = English.Text;

    string uri = "https://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + HttpUtility.UrlEncode(text) + "&from=" + "en" + "&to=" + "ja";
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
    httpWebRequest.Headers.Add("Authorization", authToken);
    using (WebResponse response = httpWebRequest.GetResponse())
    using (Stream stream = response.GetResponseStream())
    {
        DataContractSerializer dcs = new DataContractSerializer(Type.GetType("System.String"));
        string translation = (string)dcs.ReadObject(stream);
        Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "ja");
        Console.WriteLine(translation);
        output = translation;
    }
    Japanese.Text = output;

}

class Erwin : Translator2
{
    string from, to;
    public void SwapStrings(string s1, string s2)

{

    string temp = s1;
    s1 = s2;
    s2 = temp;



    from = s1;
    to = s2;

    testing.Text = from;
    testing1.Text = to;

}

protected void Switch(object sender, EventArgs e)
{
    string str1 = testing.Text;
    string str2 = testing1.Text;
    System.Console.WriteLine("Inside Main, before swapping: {0} {1}", str1, str2);

    SwapStrings(str1, str2);
}

}

你知道为什么我不能添加Erwin:Translato2课程吗?在我添加课程之前一切正常。我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

Switch方法与为aspx页面提供动力的类不同。如果你想让switch成为某种实用方法,你应该在一个完全独立的文件(Erwin.cs)中创建一个新类,可能是Erwin,并且在其中有一个switch方法是公共的可能是静态的。我还注意到switch没有返回任何内容,所以不确定你要用它来完成什么。

按钮的OnClick处理程序应该在后面的代码中 - 即在Translator2类中。在此处理程序中,您可以调用Erwin.Switch()