我的ASP.Net MVC应用程序出了问题。在Service文件夹中,我创建了我的WCF服务:
IService.cs
namespace HelloWorld
{
[ServiceContract]
public interface IService
{
[OperationContract]
List<string> GetUsers();
}
}
Service.svc
<%@ ServiceHost Language="C#" Debug="true" Service="HelloWorld.Service" CodeBehind="Service.svc.cs" %>
Service.svc.cs
namespace HelloWorld
{
public class Service : IService
{
private ApplicationDbContext db = new ApplicationDbContext();
public List<string> users = new List<string>();
public List<string> GetUsers()
{
foreach(var item in db.Users.ToList())
{
users.Add(item.UserName.ToString());
}
return users;
}
}
}
之后,我将服务引用添加到我的MVC客户端,并为我的控制器创建了以下操作:
// POST: SimpleClient/Create
[HttpPost]
public ActionResult Index()
{
UserService.ServiceClient objUser = new UserService.ServiceClient();
ViewBag.Users = objUser.GetUsers().ToList(); // null
return View();
}
但在我的索引视图中,我在ViewBag中获得了null值。我在哪里弄错了?