我正在创建一个WCF服务并在VS.NET 2013中本地运行它。我可以很好地导航到svc文件,但是当我尝试调用其中一个方法时,我收到了404响应。我感谢你们所有的想法。
这是接口文件:
public interface ITechManager
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<Tech> GetTechListings();
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Tech GetTechDetails(int id);
}
这是.svc文件:
public class TechManager : ITechManager
{
public List<Tech> GetTechListings()
{
List<Tech> techs = GetTech(0);
return techs;
}
public Tech GetTechDetails(int id)
{
List<Tech> techs = GetTech(id);
if (techs.Count > 0)
{
return techs[0];
}
else
{
return new Tech();
}
}
private List<Tech> GetTech(int id)
{...}
这是web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.5.1" />
<httpRuntime targetFramework="4.5.1" />
</system.web>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding>
<security mode="None"></security>
</binding>
</webHttpBinding>
</bindings>
<services>
<service name="testing.TechManager" behaviorConfiguration="webBehavior">
<endpoint address="TechManager.svc" binding="webHttpBinding" contract="testing.ITechManager"></endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="webBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"
multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
这是我用来调用方法的Javascript:
<script type="text/javascript">
function GetListing() {
$.ajax({
type: "POST",
url: "TechManager.svc/GetTechListings",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
var itemRow = "< table >";
$.each(data, function (index, item) {
itemRow += "<tr><td>" + item.ID + "</td><td>" + item.Title + "</td></tr>";
});
itemRow += "</table>";
$("#items").html(itemRow);
}
});
}
function GetDetails() {
$.ajax({
type: "POST",
url: "TechManager.svc/GetTechDetails",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{ID: 321}",
success: function (data) {
var itemRow = "< table >";
$.each(data, function (index, item) {
itemRow += "<tr><td>" + item.ID + "</td><td>" + item.Title + "</td></tr>";
});
itemRow += "</table>";
$("#items").html(itemRow);
}
});
}
</script>