如何在URL的末尾添加参数

时间:2015-03-17 11:35:16

标签: c# wcf

我在c#中创建了一个wcf服务。我成功发布了它。现在我想在URL中传递参数,而wcf将以json格式返回数据。但是我正在尝试而不是我没有得到任何答案。你能不能帮助我如何在URL的末尾添加参数。

IwcfDataService.cs

namespace RestWCFService
{

    [ServiceContract]
    public interface IWcfDataService
    {
        [OperationContract]
        [WebInvoke(Method = "GET",
         ResponseFormat = WebMessageFormat.Json,
         BodyStyle = WebMessageBodyStyle.Wrapped,
         UriTemplate = "Search?keyword={keyword}")]
        List<string> Search(string keyword);
    }

    [DataContract]
    public class Detail
    {

        [DataMember]
        public string InnerText { get; set; }

        [DataMember]
        public string URL { get; set; }

        [DataMember]
        public string Title { get; set; }


    } 
}

WcfDataService.svc.cs

namespace RestWCFService
{

    public class WcfDataService : IWcfDataService
    {

        public List<string> Search(string keyword)
        {
                 SqlConnection sql_Search;


                 sql_Search = new SqlConnection("Data Source=FPBLWD0310;Initial Catalog=Support;Integrated Security=True");
                 sql_Search.Open();
                 SqlCommand SelectCommand = new SqlCommand("SELECT KeyWords,URLName,Title FROM finalCrawler Where (KeyWords Like '%" + keyword + "%')", sql_Search);


                 SelectCommand.ExecuteNonQuery();
                 SqlDataAdapter SQLAd = new SqlDataAdapter(SelectCommand);
                 SqlDataReader read = SelectCommand.ExecuteReader();
                 Detail De = null;
                 List<string> ListDetail = new List<string>();
                 while (read.Read())
                 {
                      De = new Detail { 
                       InnerText = read["KeyWords"].ToString(),
                       URL = read["URLName"].ToString(),
                       Title  = read["Title"].ToString()
                    };
                      var javaScriptSerializer = new
     System.Web.Script.Serialization.JavaScriptSerializer();
                      string jsonString = javaScriptSerializer.Serialize(De);
                      ListDetail.Add(jsonString);
                 }

                 sql_Search.Close();  


           return ListDetail;

        }

URL 所以你能告诉我如何在URL的末尾添加参数,以便得到结果

 http://localhost:8434/WcfDataService.svc?singleWsdl

1 个答案:

答案 0 :(得分:1)

只需使用它来构建您的网址参数:

url += string.Format("&TabName={0}", HttpUtility.UrlEncode(tabName));