Reputation: 393
I am trying to call a method in my Api Service:
public IQueryable Get(int UserId){
return UsersRepository.SelectAll().Where(ig => ig.Id == UserId);
}
from my HomeController:
UsersService.Get(UserId);
but I get this error: An object reference is required for the non-static field, method, or property 'CTHRC.Roti.Domain.Api.Services.UsersService.Get(int)'
What am I doing wrong? Here is my UserService:
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);
}
}
}
and here is my Home Controller:
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();
}
}
}
here is my IUsersRespository:
using System;
using System.Linq;
using CTHRC.Roti.Domain.Model;
namespace CTHRC.Roti.Domain.Data.Repositories
{
public interface IUsersRepository : IRepository<Users>
{
}
}
Upvotes: 0
Views: 218
Reputation: 149538
You are trying to call an instance method like a static method.
You have to instansiate UsersService
in order to access the Get
method:
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();
}
}
Upvotes: 3