简体   繁体   English

从C#将参数传递到WCF REST

[英]Pass parameters to WCF REST from C#

I have to pass the parameters to the WCF service that i created. 我必须将参数传递给我创建的WCF服务。 When i tried to pass it as a QueryString it throwed me " 404 Not found ". 当我尝试将其作为QueryString传递时,它抛出了“ 404 Not found ”。

//Code: //码:

string Service = ""http://localhost:58092/Service1.svc/DataService/LoadAllColumnData?Id=1";
WebRequest wreq = WebRequest.Create(Service);

WebResponse wres = wreq.GetResponse();

DataContractSerializer coll = new DataContractSerializer(typeof(DataServiceProxy.GdColumns));                      
var arrProd = coll.ReadObject(wres.GetResponseStream());

//WCF Code: // WCF代码:

[ServiceContract]
public interface IDataService
{             
     [OperationContract]
     [WebGet(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "LoadAllColumnData/{Id}")]
     IList<GdColumns> LoadAllColumnData(string Id);
}

How to do this? 这个怎么做?

In your string Service you specify a querystring LoadAllColumnData?Id=1 ... but in your UriTemplate = "LoadAllColumnData/{Id}" you have no querystring... you have defined that the URL will hold the parameter. 在您的字符串Service中,您指定了一个查询字符串LoadAllColumnData?Id = 1 ...,但是在您的UriTemplate =“ LoadAllColumnData / {Id}”中,您没有查询字符串...您已定义URL将保存该参数。

Change your service url as follows and it should work... 如下更改您的服务网址,它应该可以工作...

string Service = ""http://localhost:58092/Service1.svc/DataService/LoadAllColumnData/1";

To use the querystring... 要使用查询字符串...

[WebInvoke(Method = "GET",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    UriTemplate = "LoadAllColumnData?Id={Id}")]
IList<GdColumns> LoadAllColumnData(string Id);

To use the URL... 要使用网址...

[WebInvoke(Method = "GET",
    BodyStyle = WebMessageBodyStyle.WrappedRequest,
    UriTemplate = "LoadAllColumnData/{Id}")]
IList<GdColumns> LoadAllColumnData(string Id);

Edit: Changed POST to GET as Derek pointed out. 编辑:正如德里克指出的,将POST更改为GET。

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

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