WCF REST 4.0 PUT / POST / DELETE不工作(400错误的请求) [英] WCF REST 4.0 PUT/POST/DELETE not working (400 Bad Request)

查看:174
本文介绍了WCF REST 4.0 PUT / POST / DELETE不工作(400错误的请求)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力在.NET 4.0中建立一个WCF REST服务。我有GET请求的工作,但涉及的数据发布到服务器的任何请求失败了 HTTP 400错误的请求

这是我的简单服务:

  [的ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)
公共类服务1
{
    [WebGet(UriTemplate =)]
    公共字符串的HelloWorld()
    {
        返回的Hello World;
    }    [WebInvoke(UriTemplate =,方法=POST)]
    公共字符串HelloWorldPost(字符串名称)
    {
        返回你好+名称;
    }
}

我的web.config:

 <?XML版本=1.0&GT?;
<结构>  <&的System.Web GT;
    <编译调试=真targetFramework =4.0/>
  < /system.web>  < system.webServer>
    <模块runAllManagedModulesForAllRequests =真正的>
      <添加名称=UrlRoutingModuleTYPE =System.Web.Routing.UrlRoutingModule,System.Web程序,版本= 4.0.0.0,文化=中性公钥= b03f5f7f11d50a3a/>
    < /模块>
  < /system.webServer>  < system.serviceModel>
    < serviceHostingEnvironment aspNetCompatibilityEnabled =真/>
    < protocolMapping>
      <加入计划=HTTP绑定=的WebHttpBinding/>
    < / protocolMapping>
    < standardEndpoints>
      < webHttpEndpoint>
        <! -
            通过的Global.asax.cs文件和缺省端点配置WCF REST服务的基地址
            经由上的&lt属性; standardEndpoint>下面元素
         - >
        < standardEndpoint NAME =helpEnabled =真automaticFormatSelectionEnabled =真/>
      < / webHttpEndpoint>
    < / standardEndpoints>
  < /system.serviceModel>< /结构>

和我的Global.asax:

 公共类全球:一个HttpApplication
{
    无效的Application_Start(对象发件人,EventArgs的发送)
    {
        的RegisterRoutes();
    }    私人无效的RegisterRoutes()
    {
        RouteTable.Routes.Add(新ServiceRoute(服务1,新WebServiceHostFactory()的typeof(服务1)));
    }
}

基本上,一切都是从模板默认,但我只是简化了服务1 。我曾经尝试都运行它通过调试,并通过一个请求通过的提琴手和IIS运行它,做同样的,以及使用一个简单的控制台应用程序,以伪造的职位,但我总是得到 400错误请求错误,我不知道为什么。我看了所有在互联网上,并不能推测出任何东西。

我试过以下两个要求的例子(没有工作):

XML:

 <字符串的xmlns =htt​​p://schemas.microsoft.com/2003/10/Serialization/>字符串的内容和LT; /串>

JSON:

 字符串内容


解决方案

您正确设置内容类型头在你的要求?对于XML请求它应该是文本/ XML 并为JSON它应该是应用程序/ JSON 。您code工作对我来说,当我在提琴手设定的内容类型。

您也应该设置接受头在你的GET为文本/ XML 应用程序/ JSON 取决于你想在响应的格式。这是正常的POST作为服务器会认为你要在相同的格式请求的响应,因为你有集 automaticFormatSelectionEnabled =在你的web.config真实。有一个关于在WCF REST这里格式选择更多详细信息:<一href=\"http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx\">http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx

I have been working on setting up a WCF REST service in .NET 4.0. I have GET requests working, but any request that involves POSTing data to the server fails with a HTTP 400 Bad Request.

This is my simple service:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class Service1
{
    [WebGet(UriTemplate = "")]
    public string HelloWorld()
    {
        return "hello world";
    }

    [WebInvoke(UriTemplate = "", Method = "POST")]
    public string HelloWorldPost(string name)
    {
        return "hello " + name;
    }
}

My Web.config:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </modules>
  </system.webServer>

  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <protocolMapping>
      <add scheme="http" binding="webHttpBinding" />      
    </protocolMapping>
    <standardEndpoints>
      <webHttpEndpoint>
        <!-- 
            Configure the WCF REST service base address via the global.asax.cs file and the default endpoint 
            via the attributes on the <standardEndpoint> element below
        -->
        <standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
      </webHttpEndpoint>
    </standardEndpoints>
  </system.serviceModel>

</configuration>

And my global.asax:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
    }
}

Basically, everything is default from the template, but I've just simplified the Service1. I have tried both running it through the debugger and passing a request through Fiddler and running it in IIS and doing the same, as well as using a simple console application to fake the POST but I always get the 400 Bad Request error and I have no idea why. I've looked all over the internet and can't figure anything out.

I've tried both of the following request examples (neither work):

XML:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">String content</string>

JSON:

"String content"

解决方案

Are you setting the Content-Type header correctly in your request? For the XML request it should be text/xml and for the JSON it should be application/json. Your code works for me when I set the Content-Type in Fiddler.

You should also set the Accept header in your GET to either text/xml or application/json depending on what format you want the response to be in. It is okay for the POST as the server will assume that you want the response in the same format as the request, because you have set automaticFormatSelectionEnabled="true" in your web.config. There's more detail about format selection in WCF REST here: http://blogs.msdn.com/b/endpoint/archive/2010/01/18/automatic-and-explicit-format-selection-in-wcf-webhttp-services.aspx

这篇关于WCF REST 4.0 PUT / POST / DELETE不工作(400错误的请求)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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