使用WCF HTTP的Web API,UriTemplateMatch始终为null [英] Using the WCF HTTP Web API, UriTemplateMatch is always null

查看:354
本文介绍了使用WCF HTTP的Web API,UriTemplateMatch始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置我的服务,根据我已经找到了最新的教程,一切似乎很好地工作。

I've setup my service according to the latest tutorials I've found, and everything seems to work fine.

不过,

为了访问包含QueryParameters集合WebOperationContext.Current.IncomingRequest.UriTemplateMatch类(如姓名=汤姆和放大器;年龄= 20),我需要有配置为使用WebHttpBehavior的服务。我已经成功地得到这个工作的唯一方法是将其用WebServiceHost从控制台应用程序的自我主机。我不能让它从web.config中工作或IIS或卡西尼Global.asax中。

In order to access the WebOperationContext.Current.IncomingRequest.UriTemplateMatch class which contains the QueryParameters collection (e.g. ?name=tom&age=20), I need to have the service configured to use WebHttpBehavior. The only way I've managed to get this to work is to self host it using WebServiceHost from a console application. I can't get it to work from the web.config or global.asax from IIS or cassini.

我觉得奇怪的是如何承载的东西在IIS之前,使用有关的IoC的网络API通话教程:那不是更加有用吗?他们似乎都使用根本不使用查询字符串非常简单的服务,国际奥委会!

I find it strange that tutorials on how to use the web-api talk about IoC before hosting the thing in IIS: wouldn't that be far more useful? They all seem to use extremely simple services that don't use query strings at all, with IoC!

下面是我发现,几乎提到这个问题,但不解决它的资源:

Here are the resources I've found that almost mention the problem but don't fix it:

  • http://social.msdn.microsoft.com/Forums/nl-NL/wcf/thread/33861d74-9037-4d4f-836b-efe715de5af3
  • http://pfelix.wordpress.com/2011/04/22/wcf-web-apiiis-hosting/

推荐答案

您可以做这样的事情:

[ServiceContract]
public class ContactResource {
    [WebGet(UriTemplate = "")]
    public HttpResponseMessage<Contact> Get(HttpRequestMessage request) {
        var querystring = request.RequestUri.Query;
        var parameters = HttpUtility.ParseQueryString(querystring);
        var name = parameters["Name"];
        return new HttpResponseMessage<Contact>(
            new Contact()
                {
                    Id = Guid.NewGuid(),
                    Name = name
                });
    }
}

的http://本地主机:12741 /联系人姓名=乔治

收益率:

<Contact>
<Id>19bae3a5-e2b7-4858-8aa4-08161ea18018</Id>
<Name>George</Name>
</Contact>

这篇关于使用WCF HTTP的Web API,UriTemplateMatch始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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