【问题标题】:Call Api Service from HomeController从 HomeController 调用 API 服务
【发布时间】:2014-06-26 19:24:35
【问题描述】:

我正在尝试在我的 Api 服务中调用一个方法:

public IQueryable Get(int UserId){

     return UsersRepository.SelectAll().Where(ig => ig.Id == UserId);

}

来自我的 HomeController:

UsersService.Get(UserId);

但我收到此错误:An object reference is required for the non-static field, method, or property 'CTHRC.Roti.Domain.Api.Services.UsersService.Get(int)'

我做错了什么?这是我的用户服务:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CTHRC.Roti.Domain.Data.Repositories;
using CTHRC.Roti.Domain.Model;

namespace CTHRC.Roti.Domain.Api.Services
{
    public class UsersService
    {
        protected readonly IUsersRepository UsersRepository;

        public UsersService(IUsersRepository userRespository)
        {
            UsersRepository = userRespository;
        }

        public IQueryable Get(int UserId)
        {
            return UsersRepository.SelectAll().Where(ig => ig.Id == UserId);
        }
    }
}

这是我的家庭控制器:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;
using CTHRC.Roti.Domain.Model;
using CTHRC.Roti.Domain.Api.Services;
using CTHRC.Roti.Domain.Data.Repositories;

namespace CTHRC.Roti.Web.UI.Controllers
{
    public class HomeController : Controller
    {

        public ActionResult Index()
        {
            if (!WebSecurity.IsAuthenticated)
            {
                Response.Redirect("~/account/login");
            }
            int UserId = 1;
            UsersService.Get(UserId);
            return View();

        }

    }
}

这是我的 IUsersRespository:

using System;
using System.Linq;
using CTHRC.Roti.Domain.Model;

namespace CTHRC.Roti.Domain.Data.Repositories
{
    public interface IUsersRepository : IRepository<Users>
    {

    }

}

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc-4


    【解决方案1】:

    您正在尝试像调用静态方法一样调用实例方法。 您必须实例化 UsersService 才能访问 Get 方法:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            if (!WebSecurity.IsAuthenticated)
            {
                Response.Redirect("~/account/login");
            }
    
            int UserId = 1;
    
            var service = new UsersService(userRepository);
            service.Get(UserId);
    
            return View();
    
        }
      }
    

    【讨论】:

    • 虽然看起来 OP 正在使用某种形式的 DI 容器,所以最好使用它来获取 UserService 实例。例如var service = kernel.Get&lt;UserService&gt;()(或一些传真)
    • 这给了我另一个错误....当前上下文中不存在名称'userRepository'
    • 我没有使用 DI 容器的 OP 示例,所以我更愿意让它清晰简单。如果他使用一个,他可以随心所欲地实例化它
    • userRepository 只是一个示例,因为UsersService 需要在其构造函数中接收IUserRepository。你使用任何依赖注入容器吗?
    • kernel.Get&lt;UserService&gt;() 给我一个错误'名称内核'在当前上下文中不存在'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-16
    • 2020-10-22
    相关资源
    最近更新 更多