WCF REST 服务返回 HTTP 400 错误请求 [英] WCF REST Service returns HTTP 400 Bad Request

查看:73
本文介绍了WCF REST 服务返回 HTTP 400 错误请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试创建一个简单的 WCF RESTful Web 服务,但它似乎只能在 SOAP 模式下工作.我使用本地 IIS 托管我的服务.

I've been trying to create a simple WCF RESTful web service, but it seems to work only in SOAP mode. I'm hosting my service with the local IIS.

看起来很标准:

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    Guid Login(string username, string password);
...

和:

public class MyService : IMyService
{
    [WebGet(UriTemplate = "login?user={user}&password={password}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
    public Guid Login(string username, string password)
    {
        return Guid.Empty;
    }

我还在配置文件中包含了该行为,并在:

I also included the behavior in the config file and was using it in:

<endpoint address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="webHttp"/>

根据我知道的所有例子...

according to all of the examples I know...

现在的问题是当从使用 ServiceReference 的存根客户端调用登录时,它在 Fiddler 中看起来很好,但它是 SOAPy.由于某种原因,我无法以 RESTy 方式调用我的服务,甚至/help 似乎也返回 400 Bad Request.(我正在调用 http://localhost:8080/MyService.svc/help 或/login等)

Now the thing is when invoking the login from a stub client which uses ServiceReference, it looks just fine in Fiddler, but it is SOAPy. From some reason I cannot invoke my service in a RESTy way, even the /help seems to return a 400 Bad Request. (I'm invoking http://localhost:8080/MyService.svc/help or /login etc.)

是什么阻止了 REST 采取行动?提前致谢:)

What is preventing the REST to take action? Thanks in advance :)

我找到了答案.

事实证明必须定义路由...

It turns out one must define Routings...

将此添加到 Global.asax 后:

After adding this to Global.asax :

    protected void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.Add(new ServiceRoute("MyService",
           new WebServiceHostFactory(), typeof(MyService)));
    }

一切顺利.

另外,.svc"现在不是 URL 的一部分.

Plus I earned that the ".svc" is now not part of the URL.

推荐答案

我遇到了同样的问题原因是我发布的 xml 有以下内容

I had the same issue the reason was my posting xml had the below on top

<?xml version="1.0" encoding="utf-16"?>

我删除了它,它就起作用了!

I removed it and it just worked!

这篇关于WCF REST 服务返回 HTTP 400 错误请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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