WCF 400错误的请求
问题描述:
我创建使用Javascript/jQuery的一个简单的函数WCF 400错误的请求
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
string Start();
定义,
public String Start()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize("Check");
}
从浏览器, http://localhost/service1.svc告诉我,我已经创建了一个服务和其他所有信息..看起来很好。
我试图把这种使用 http://localhost/service1.svc/Start
我得到这个呼叫的400错误的请求。我希望我在这里做的事情没有完全错误。我应该能够从浏览器访问WCF服务吗? 在我想要发布之前,我尝试了很多。但我无法得到这个基本的东西令我感到沮丧。
编辑& UPDATE 现在我在这个阶段。该服务页面告诉我,元数据服务被禁用,并要求我插入下面的文本
<serviceMetadata httpGetEnabled="true" />
我插入的文本 - 但它毕竟显示了相同的文字!现在,这是越来越太混乱..
答
为我工作。我创建了WCF Rest服务。
我使用的URL看起来像http://localhost:8080/Service1/Start
下面是代码:
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Script.Serialization;
namespace WcfRestService1
{
// Start the service and browse to http://<machine_name>:<port>/Service1/help to view the service's generated help page
// NOTE: By default, a new instance of the service is created for each call; change the InstanceContextMode to Single if you want
// a single instance of the service to process all calls.
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class Service1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json)]
public string Start()
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize("Check");
}
}
}
你有与状态代码的任何有效载荷?如果否,请尝试打开将错误消息发送回客户端的选项。它在ServiceBehaviors/Behavior元素下是。 –
它设置为true。但我没有得到任何有效载荷。只是状态码。 – Tom
@ a.dimo是对的,你指定你的方法只能用POST方法调用。当您使用浏览器请求时,它会发送GET请求。 –