【发布时间】:2011-04-23 05:09:49
【问题描述】:
我在我的项目中创建了一个 WCF REST 样式服务。在开发中,我可以指定相对 url 来访问我的服务,
jQuery.ajax({
url: "/WebServices/AddressWebService.svc/GetCountry",
datatype: "json",
success: function (data) {
jQuery.each(data.GetCountryResult, function (index, value) {
//value.entityData.CountryGuid
//value.entityData.CountryName
$('#dataCountry').empty();
$('#dataCountry').append('<option value="' + value.entityData.CountryGuid + '">' + value.entityData.CountryName + '</option>');
});
}
});
当我使用根文件夹名称 c2c 托管到 IIS(使用端口 81)时,我不再能够访问该服务
使用 fire bug,我试图查看标题,我的相对 url ("/WebServices/AddressWebService.svc/GetCountry) 被附加到我的主机 (localhost:81) 但虚拟文件夹名称 (c2c) 没有被附加. 结果显示文件未找到异常
如果我使用完整的限定 url (http://localhost:81/c2c/WebServices/AddressWebService.svc/GetCountry),它可以正常工作。
如果我使用的是完全合格的 url,那么在转移到生产环境时将很难进行更改。虽然我们可以使用全局变量来存储 webroot(在本例中为 c2c)
有没有什么简单的办法可以解决这个问题,我也在config文件中给出了基地址
<system.serviceModel>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="c2c.WebServices.AddressWebService" behaviorConfiguration="jsonBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:81/c2c"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" bindingConfiguration="webmax" contract="c2c.WebServices.IAddressWebService" behaviorConfiguration="jsonendpointBehavior" />
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
</service>
</services>
【问题讨论】: