gpt4 book ai didi

c# - 从 Jquery 调用 WCF Restfull 服务

转载 作者:太空宇宙 更新时间:2023-11-03 15:47:29 25 4
gpt4 key购买 nike

我已经创建了 WCF Restfull 服务,但是当我从 JS 调用它时,它总是说找不到 Respurce。这是y员工类

namespace WcfRestMVC.Models
{
[DataContract]
public class Employee
{
[Key]
[DataMember]
public int Id { get; set; }
[DataMember]
public string FirstName { get; set; }
[DataMember]
public string LastName { get; set; }

[DataMember]
public string Title { get; set; }
}
}

服务接口(interface)

namespace WcfRestMVC
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IEmployeeService" in both code and config file together.
[ServiceContract]
public interface IEmployeeService
{
[OperationContract]
[WebGet(UriTemplate = "Employees", ResponseFormat = WebMessageFormat.Json)]
Employee[] GetAllEmployee();

[OperationContract]
[WebGet(UriTemplate = "EmployeeById/{id}", ResponseFormat = WebMessageFormat.Json)]
Employee GetEmployeeByID(string Id);
}
}

我的服务

namespace WcfRestMVC
{
public class EmployeeService : IEmployeeService
{
public Employee[] GetAllEmployee()
{
using (DemoContext Dbobj = new DemoContext())
{
var getAllEmployee = Dbobj.eployees.ToList();
return getAllEmployee.ToArray();
}
}


public Employee GetEmployeeByID(string Id)
{
using (DemoContext Dbobj = new DemoContext())
{
var GetEmployeeById = Dbobj.eployees.FirstOrDefault(m => m.Id == Convert.ToInt32(Id));
return GetEmployeeById;
}
}
}
}

我的 web.config 文件为

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<services>
<service name="WcfRestMVC.EmployeeService">
<endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding" contract="WcfRestMVC.IEmployeeService" />
</service>
</services>
</system.serviceModel>

我在 JS 中调用它

$.ajax({
type: "GET",
url: "http://localhost:61716/EmployeeService.svc/Employees",
contentType: "json",
dataType: "json",
success: function (data) {
var d = data;
},
error: function (xhr) {
var s=xhr;
}
});

在我的浏览器中粘贴此 url 时也是如此

http://localhost:61716/EmployeeService.svc/Employees

它说找不到资源。。我认为我的 web.config 文件有问题。你能帮帮我吗?

最佳答案

关于c# - 从 Jquery 调用 WCF Restfull 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27490477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com