简体   繁体   English

如何在WCF REST服务中传递url?

[英]How do I pass url in WCF REST Service?

I am creating the following WCF REST Service: 我正在创建以下WCF REST服务:

MY Interface: 我的界面:

namespace WcfService1
{   
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/InsertData/{data}")]
        string InsertData(Data data);
    }

    [DataContract]
    public class Data
    {
        [DataMember]
        public string Name { get; set; }
        [DataMember]
        public string Email { get; set; }
        [DataMember]
        public string Category { get; set; }
        [DataMember]
        public string Mobile { get; set; }
        [DataMember]
        public string Message { get; set; }
    }
}

Now how do I pass the values in the URL. 现在,我如何在URL中传递值。 If I am running the current application, the following error is coming: 如果我正在运行当前应用程序,则会出现以下错误:

在此处输入图片说明

You are trying to invoke InsertData method which needs complex object Data to be passed to it and you use Get method to do it. 您试图调用InsertData方法,该方法需要将复杂的对象数据传递给它,然后使用Get方法来执行此操作。 Generally it is a bad idea as Get request do not have body content to store serialized Data object - instead in Get you can pass data only as query string. 通常,这是个坏主意,因为Get请求没有正文内容来存储序列化的Data对象-相反,在Get中,您只能将数据作为查询字符串传递。 For InsertData method the proper choice of http method would be Put. 对于InsertData方法,应正确放置http方法。 If you want to test your method with browser prepare method like Get(int id), specify propert UriTemplate to "data/Get/{id}" and then call from your browser yourserviceurl/Service1.svc/data/Get/1 如果要使用诸如Get(int id)之类的浏览器准备方法来测试您的方法,则将属性UriTemplate指定为“ data / Get / {id}”,然后从浏览器中调用yourserviceurl / Service1.svc / data / Get / 1

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

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