我已经创建了两种方法之一是获取,另一个是创建,在WCF REST服务插入数据表示错误:远程服务器返回一个错误:(404)未找到
对于获取数据工作正常,但是当我试图发布数据显示错误如Method not Found(404)
该代码如下。
合同方法:
public interface IContactPositionService {
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, Method = "GET",RequestFormat=WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetContactPositionList")]
List<ContactPosition> GetContactPositionList();
[OperationContract]
[WebInvoke(Method = "PUT", BodyStyle = WebMessageBodyStyle.WrappedRequest,RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "AddContactPosition")]
int AddContactPosition(ContactPosition position);}
和通讯Serivice类是这样的:
public class ContactPositionService : IContactPositionService
{
public List<ContactPosition> GetContactPositionList()
{
DataSet ds = clsObj.GetContactPositionsList();
return ConverterHelper.Convert<ContactPosition>(ds.Tables[0]);
}
public int AddContactPosition(ContactPosition position)
{
// throw new System.NotImplementedException();
}
而且在Web.config文件我已经配置端点为:
<services>
<service name="VirtusMobileService.ContactPositionService" behaviorConfiguration="VMS.Position.ServiceBehaviour">
<endpoint address="" behaviorConfiguration="VMS.Position.EndPointBehaviour"
binding="webHttpBinding" bindingConfiguration="" name="VMS.Position.EndPoint"
contract="VirtusMobileService.IContactPositionService" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="VMS.Position.EndPointBehaviour">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="VMS.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="VMS.Position.ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
从我们称客户端Asp.net页面为添加联系人对象的服务,如下所示:
private void AddData()
{
string sURL = "http://localhost:51293/ContactPositionService.svc/AddContactPosition";
ContactPosition order = new ContactPosition
{
PositionCode="10550",
PositionFinbaseId=11,
PositionTitle="Manager"
};
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(ContactPosition));
MemoryStream mem = new MemoryStream();
ser.WriteObject(mem, order);
string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
WebClient webClient = new WebClient();
webClient.Headers["Content-type"] = "application/json";
webClient.Encoding = Encoding.UTF8;
webClient.UploadData(sURL, "PUT", mem.ToArray()); // Here it is showing Error
Console.WriteLine("Order placed successfully...");
}
虽然涉及到“ uploadData”它显示例如错误“ 远程服务器返回错误:(404)未找到”
If i am trying to Get the Data using the "GetContactPositionList"
它显示的数据正确,但是当我试图用 “PUT”方法则显示出错误的工作。
我测试在Fiddler中的URL,它显示了同样的错误响应 部首“HTTP/1.1 405不允许的方法”
<div id="content">
<p class="heading1">Service</p>
<p xmlns="">Method not allowed. Please see the <a rel="help-page" href="http://localhost:51293/ContactPositionService.svc/help">service help page</a> for constructing valid requests to the service.</p>
</div>
请推荐答案。
谢谢。
你得到405当方法是什么,但PUT和404与PUT?你能否确定你得到的是404而不是400? –
yest这种方法错误是405只有 –