【发布时间】:2011-03-06 15:23:55
【问题描述】:
有没有办法在没有 IIS 的情况下使用 WCF 自定义友好 URL?
特别是我希望通过我自己的 Windows 服务中托管的 app.config 来做这样的事情:
[WebGet(UriTemplate = "foo/{id}")]
public string GetFoo(string id)
{
...
}
【问题讨论】:
有没有办法在没有 IIS 的情况下使用 WCF 自定义友好 URL?
特别是我希望通过我自己的 Windows 服务中托管的 app.config 来做这样的事情:
[WebGet(UriTemplate = "foo/{id}")]
public string GetFoo(string id)
{
...
}
【问题讨论】:
是的,当然 - 在您自己的 NT 服务中托管它,并在您的 app.config 中定义一个 http 基地址。您在服务合同中定义的 URI 模板将不在该基址:
<services>
<service name="YourNamespace.YourServiceClass">
<host>
<baseAddresses>
<add baseAddress="http://YourServer:9091/Services/" />
</baseAddresses>
</host>
<endpoint address=""
binding="webHttpBinding"
contract="YourNamespace.IYourService" />
</service>
</services>
然后你的 URI 模板将被添加到这个基地址,所以在这种情况下,你的 GetFoo 方法可以在以下位置调用:
http://YourServer:9091/Services/foo/42
更新:我刚刚在这里重新创建了这个,问题是这样的:你的 URI 模板定义了一个名为 {id} 的参数,但是你应用这个 URI 模板的方法没有任何参数在它的参数列表中调用id:
[WebGet(UriTemplate = "foo/{id}")]
public string GetFoo(string fooID)
您需要确保这些东西匹配!使用给定的 URI 模板,您需要将方法声明更改为:
[WebGet(UriTemplate = "foo/{id}")]
public string GetFoo(string id)
(查看参数 - 它的名称现在是 id)然后你应该没问题 - 至少它对我来说很好。
【讨论】:
<system.webServer> <modules runAllManagedModulesForAllRequests="true"> <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b13f5f7f11d50a3a" /> </modules> </system.webServer>