繁体   English   中英

Web服务和表单翻译器

[英]Web service and form translator

我的项目是编写一个Web服务和一个使用它的Web表单。 它应该有两个文本框和一个按钮。 用户在第一个文本框中输入文本口语首字母缩写词,然后按下按钮。 该Web服务将textbox1条目与字典文件进行比较,并在第二个文本框中显示生成的完整单词。 这是我到目前为止所拥有的代码,我真的在努力使其工作,任何帮助将不胜感激。 此时,我出现“类型或名称空间定义,或预期文件结尾”错误。 这是我的两个文件。

Default.aspx.cs:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class _Default : System.Web.UI.Page
{
    private Dictionary<string, string> _dictionary = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
    protected void Page_Load(object sender, EventArgs e)
    {
        using (var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
        {
            while (!reader.EndOfStream)
            {
                string[] tokens = reader.ReadLine().Split(';');
                _dictionary[tokens[0]] = tokens[1];
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        localhost.Service obj = new localhost.Service();
        TextBox1.Text = (obj.Translate());
    }
}

Service.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.IO;


[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]

public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string Translate(string input)
{
    string output;
    if(_dictionary.TryGetValue(input, out output))
        return output;

    // Obviously you might not want to throw an exception in this basis example,
    // you might just go return "ERROR".  Up to you, but those requirements are
    // beyond the scope of the question! :)
    throw new Exception("Sinatra doesn't know this ditty");
}
    }

}

不知道问题是否仍然没有答案。 但是这是我的建议。

在Web服务文件中,例如说Service1.cs,您未在声明_dictionary对象。 因此,您将在服务的构造函数中移动字典对象的声明和初始化。

下面是类似的东西。

 public WebService1()
    {
        using (var reader = new StreamReader(File.OpenRead(@"C:/dictionary.csv")))
        {
            while (!reader.EndOfStream)
            {
                string[] tokens = reader.ReadLine().Split(',');
                _dictionary[tokens[0]] = tokens[1];
            }
        }
    }

同样在split方法中,我假设您想使用逗号而不是分号(在示例中使用了分号)。

然后,在使用服务时,您将在下面执行以下操作。 我不确定您要使用样本中的localhost对象试图做什么。

 ServiceReference1.WebService1SoapClient obj = new WebService1SoapClient();
        TextBox2.Text =  obj.Translate(TextBox1.Text);

希望这可以帮助。

-达沃德

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM