WCF REST Service url
I have created a WCF REST style service in my project. In development I am able to specify the relative url to access my service,
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();
开发者_C百科 $('#dataCountry').append('<option value="' + value.entityData.CountryGuid + '">' + value.entityData.CountryName + '</option>');
});
}
});
When I hosted to IIS (with port 81), with the root folder name c2c, I am no longer able to access the service
Using fire bug, I tried to see the headers, my relative url ("/WebServices/AddressWebService.svc/GetCountry) gets appended to my host (localhost:81) but the virtual folder name (c2c) is not getting appended. As a result it shows file not found exception
If I use the full qualified url (http://localhost:81/c2c/WebServices/AddressWebService.svc/GetCountry), it works fine.
If I am using full qualified url, It will be difficult to do changes when moving to production. Although we can use global variables to store the webroot (in this case c2c)
Are there any simple solution to solve this problem, I have also given the base address in the config file
<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>
You need to get a relative URL to the call to the ajax
method. Typically what I do is
$.ajax({ url: "<%=ResolveUrl("~/Services/Service.asmx/ServiceMethod") %>" });
(obviously, you'd need the rest of the params to be set, and this would have to be on an ASP .NET control (like a page)).
However, if you're not spitting out the JavaScript on the page, that won't work. In that case, you'll want to pass in the URL of your web service to a JavaScript object or namespace, which is defined in the JavaScript include file. So you might have something like this:
MyServiceMethodCaller.serviceUrl = "<%=ResolveUrl("~/Services/Service.asmx/ServiceMethod") %>";
where you define the MyServiceMethodCaller
namespace in your JavaScript include file. That way, inside your include file, you could have:
var MyServiceMethodCaller = {
serviceUrl: "",
serviceMethod: function() {
$.ajax({ url: this.serviceUrl });
}
}
I think this should work. I haven't brought it up in a tested, workable example, but you get the gist of it.
Try changing your base address to have a trailing slash. e.g.
<baseAddresses>
<add baseAddress="http://localhost:81/c2c/"/>
</baseAddresses>
Is your entire site in the /c2c/
virtual path locally, for localhost testing, but will be deployed to run at /
in production? If so, the easiest thing to do is to make your localhost environment match production (i.e. remove the /c2c/
from the local IIS path).
If not, and /c2c/
is where everything will be in production, it's reasonable enough to use URLs like /c2c/WebServices/AddressWebService.svc/GetCountry
in your jQuery code. Even if you needed to change them, a search/replace for url: "/c2c/
=> url: "/
is a relatively safe/easy refactoring.
精彩评论