WCF Restful - 在POST上获得404错误请求 [英] WCF Restful - Getting 404 Bad Request on POST

查看:62
本文介绍了WCF Restful - 在POST上获得404错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在服务中有四种方法:其中三种是GET,另一种是POST。我的问题是调用这个POST方法。当我调用此方法时,我总是得到404 Bad Request。如果我打电话给这个没有身体的方法,那就行。



我的代码:



MyService.svc。 cs

 [ServiceContract(Namespace =  )] 
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json,UriTemplate = user / {idUser} / garages)]
public GaragesResource GetGarages( string idUser)
{
[...]
}

[...]

[OperationContract ]
[WebInvoke(RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrappe d,UriTemplate = test)]
public BaseResource PostChecklist(BaseResource baseResource)
{
[...]
}
}







Web.config

 [...] 
< system.serviceModel >
< 服务 >
< span class =code-keyword>< service name = ChecklistService.MyService >
< 端点 address = behaviorConfiguration = ChecklistService.ServiceAspNetAjaxBehavior binding = webHttpBinding 合同 = ChecklistService.MyService / >
< / service >
< / services >
< < span class =code-leadattribute>行为 >
< endpointBehaviors >
< 行为 名称 = ChecklistService.ServiceAspNetAjaxBehavior >
< span class =code-keyword>< webHttp / >
< / behavior >
< / endpointBehaviors >
< serviceBehaviors >
< 行为 < span class =code-attribute> name = serviceBehavior >
< serviceMetadata httpGetEnabled = true httpsGetEnabled = true / >
< serviceDebug includeExceptionDetailInFaults = false / >
< / behavior >
< / serviceBehaviors >
< / behavior >
< protocolMapping >
< add binding = basicHttpsBinding scheme = https / >
< / protocolMapping >
< serviceHostingEnvironment aspNetCompatibilityEnabled = tru e multipleSiteBindingsEnabled = true / >
< / system.serviceModel >
[...]
< system.webServer >
< modules runAllManagedModulesForAllRequests = true / >
< httpProtocol >
< customHeaders >
< add name = Access-Control-Allow-Origin value = * / >
< add name = Access-Control-Allow-Headers value = * / >
< / customHeaders >
< / httpProtocol >
< / system.webServer >
[...]





BaseResource.cs

 命名空间 ChecklistService.Resources 
{
[DataContract]
public class BaseRe来源
{
[DataMember]
public bool 成功{< span class =code-keyword> get ; set ; }
[DataMember]
public string 消息{获得; set ; }

public BaseResource()
{
}
}
}





我读过我不需要明确序列化注释,但我确保这不是问题所在。





现在,客户致电:

  var  data = {
成功 true
消息 测试
}

/ * var data = {
baseResource:{
Sucess:true,
消息:测试
}
} * /


$ .ajax({
类型: POST
url: http:// localhost / Checklist / MyService / test
data:JSON.stringify(data),
contentType: text / plain
dataType: json
processData: false
成功:函数(数据,状态,jqXHR){
alert( success ... + data);
},
错误:函数(xhr){
// alert(xhr) .responseText);
}
});





contentType设置如下因为,如jquery文档所告知的,跨域调用只接受3种类型的contentType或请求的方法会改变。所以,如果我使用application / json,请求的方法将是OPTIONS,而不是POST。



我一直试图让数据有很多不同方式,没有。唯一有效的方法是发送一个空字符串。



我忘记了什么?或者我说错了什么?请帮忙。



谢谢!

解决方案

.ajax({
type:< span class =code-string> POST
url: http:// localhost / Checklist / MyService / test
data:JSON.stringify(data),
contentType: text / plain
dataType: json
processData: false
成功:函数( data,status,jqXHR){
alert( success ... + data);
},
错误:函数(xhr){
// alert(xhr。 responseText);
}
});





contentType设置如下,因为,正如jquery所做的那样策划,跨域调用只接受3种类型的contentType或请求的方法会改变。所以,如果我使用application / json,请求的方法将是OPTIONS,而不是POST。



我一直试图让数据有很多不同方式,没有。唯一有效的方法是发送一个空字符串。



我忘记了什么?或者我说错了什么?请帮忙。



谢谢!


您的代码中似乎需要进行许多更改...特别是在声明操作合同时。使用:BodyStyle = WebMessageBodyStyle.Bare而不是BodyStyle = WebMessageBodyStyle.Wrapped ......并在Web.config中进行更改,同时在客户端进行AJAX调用。幸运几乎相同的情况包含在下面的文章中,并且这些文章提供的演示样本工作正常。



第1部分: ASP.NET MVC 5中的WW RESTful服务和WebGrid - 第1部分 [ ^ ]

第2部分: ASP.NET MVC 5中的WCF RESTful服务和WebGrid - 第2部分 [ ^ ]



请比较并相应地修改您的代码。请仔细查看所有内容....(一开始,我也因为没有注意到小的变化而受到影响)甚至在那之后你会得到错误,我们可以进一步讨论。感谢。

I have in my service four methods: Three of them are GET and the other one is POST. My problem is to call this POST method. When I call this method, I always get 404 Bad Request. If I call this method with no body, works.

My codes:

MyService.svc.cs

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "user/{idUser}/garages")]
    public GaragesResource GetGarages(string idUser)
    {
        [...]
    }

    [...]

    [OperationContract]
    [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "test")]
    public BaseResource PostChecklist(BaseResource baseResource)
    {
		[...]
	}
}




Web.config

[...]
<system.serviceModel>
  <services>
    <service name="ChecklistService.MyService">
      <endpoint address="" behaviorConfiguration="ChecklistService.ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="ChecklistService.MyService" />
    </service>
  </services>
  <behaviors>
    <endpointBehaviors>
      <behavior name="ChecklistService.ServiceAspNetAjaxBehavior">
        <webHttp />
      </behavior>
    </endpointBehaviors>
    <serviceBehaviors>
      <behavior name="serviceBehavior">
        <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="false" />
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <protocolMapping>
    <add binding="basicHttpsBinding" scheme="https" />
  </protocolMapping>
  <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
[...]
<system.webServer>
  <modules runAllManagedModulesForAllRequests="true" />
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="*" />
    </customHeaders>
  </httpProtocol>
</system.webServer>
[...]



BaseResource.cs

namespace ChecklistService.Resources
{
    [DataContract]
    public class BaseResource
    {
        [DataMember]
        public bool Sucess { get; set; }
        [DataMember]
        public string Message { get; set; }

        public BaseResource()
        {
        }
    }
}



I've read that I don't need to explicit the serialization annotations, but I put it to ensure that this wasn't the problem.


And now, the client call:

var data = {
    "Sucess":true, 
    "Message":"Test"
}

/*var data = { 
    "baseResource": {
        "Sucess":true, 
        "Message":"Test"
    }
}*/

$.ajax({
	type: "POST",
	url: "http://localhost/Checklist/MyService/test",
	data: JSON.stringify(data),
	contentType: "text/plain",
	dataType: "json",
	processData: false,
	success: function (data, status, jqXHR) {
		alert("success…" + data);
	},
	error: function (xhr) {
		//alert(xhr.responseText);
	}
});



The contentType is set like that because, as informed by jquery documentation, cross domain calls only accept 3 types of contentType or the method of request would change. So, if I use "application/json", the method of request would be OPTIONS, not POST.

I've been trying to make the data in many different ways and nothing. The only way that works is sending an empty string.

I forgot something? Or I set something wrong? Please help.

Thank you!

解决方案

.ajax({ type: "POST", url: "http://localhost/Checklist/MyService/test", data: JSON.stringify(data), contentType: "text/plain", dataType: "json", processData: false, success: function (data, status, jqXHR) { alert("success…" + data); }, error: function (xhr) { //alert(xhr.responseText); } });



The contentType is set like that because, as informed by jquery documentation, cross domain calls only accept 3 types of contentType or the method of request would change. So, if I use "application/json", the method of request would be OPTIONS, not POST.

I've been trying to make the data in many different ways and nothing. The only way that works is sending an empty string.

I forgot something? Or I set something wrong? Please help.

Thank you!


It seems many changes needed in your code...specially while declaring operation contract. Use: BodyStyle = WebMessageBodyStyle.Bare instead of BodyStyle = WebMessageBodyStyle.Wrapped......and do changes in Web.config and while making AJAX call at client side. Lucky almost same scenario is covered in below articles and demo samples given with those articles are working fine.

Part 1: WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 1[^]
Part 2: WCF RESTful service and WebGrid in ASP.NET MVC 5 - Part 2[^]

Please compare and modify your code accordingly. Please look closely in everything....(In beginning, I suffered too as I was not noticing small changes) and even after that you get errors, we can discuss further. Thanks.


这篇关于WCF Restful - 在POST上获得404错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆