3

enter image description hereI'm new to the ASP.NET MVC 5 and WebAPI 2 technology. I am currently developing a web service for my desktop application.

I have developed the web service with individual user account authentication in asp.net mvc 5 web API 2. I refer link :- " http://vod.com.ng/en/video/KyxcLfz_CW8/8-Authenticated-WebAPI-ASPNET-MVC-5-Fundamentals-5-WebAPI-2 ". It helped me but I need to add layers in project i.e. WEB and CORE.

I shifted "AccountBindingModels.cs" and "AccountViewModels.cs" from Models folder in WEB to POCO folder in CORE, after running the program I am getting error " POST /api/account/register 500 (Internal Server Error) " and " An error occurred when trying to create a controller of type 'AccountController' ".

I want to add api controllers and model classes which will be authenticated by the individual user account. Please help. Let me know what else you need to know. Thanks in advance.

CODE from UnityConfig.cs file

public static void RegisterTypes(IUnityContainer container)
{
  container.RegisterType(typeof(IRepository<>), typeof(Repository<>));

  container.RegisterType(typeof(DbContext), typeof(DataContext));
}  

Controller

namespace DesktopApp.Controllers
{
public class StudentLoginController : ApiController
{
    private IRepository<StudentLogin> _StudentLoginRepository;
    public StudentLoginController(IRepository<StudentLogin> StudentLoginRepository)
    {
        _StudentLoginRepository = StudentLoginRepository;
    }

    [HttpPost]
    [Route("api/StudentLogin/Post")]
    public StudentLogin Post(StudentLogin loginData)
    {
        var studentLoginDetails = _StudentLoginRepository.GetAll().Where(p =>      p.studentName == loginData.studentName && p.studentPassword == loginData.studentPassword).FirstOrDefault<StudentLogin>();
        return studentLoginDetails;
    }     
Rushee
  • 766
  • 7
  • 18
  • You're question is little bit vague. Could you please post the appropriate code snippets? By the way, I wouldn't move the view models to another layer, because they are related to the UI so it is a good idea to leave them in this layer. – Horizon_Net Jun 20 '14 at 09:08
  • How does the resolving of your dependencies look like? Without seeing your actual injection it is hard to say what's going wrong. Maybe [this thread](http://stackoverflow.com/questions/21927785/configure-unity-di-for-asp-net-identity) helps you a little bit. – Horizon_Net Jun 20 '14 at 10:39
  • Thanks for the help. yes i moved the view models to the same layer. But I have added the model classes in another layer Core -> POCO. I am using the Dependency Injection with Unity. i think there is any issue in resolving the dependencies of controller. I think i have add manual dependency resolver for the controllers . I have added the image above. – Rushee Jun 20 '14 at 10:40
  • I am stuck in solving the dependencies between the userManager controller and my web api controllers. I have added the code above. – Rushee Jun 20 '14 at 10:59
  • How does your actual controller looks like (especially the constructors where you perform the injection)? – Horizon_Net Jun 20 '14 at 11:15
  • I have post the web api controller's code with constructor above. – Rushee Jun 20 '14 at 11:28

1 Answers1

3

Solved the issue by creating the default AccountController and deleting the parameterized AccountController.

 public UserManager<IdentityUser> UserManager { get; private set; }
    public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }

    public AccountController()            
    {
        UserManager = Startup.UserManagerFactory();
       AccessTokenFormat= Startup.OAuthOptions.AccessTokenFormat;
    }
Rushee
  • 766
  • 7
  • 18