0

I have created a new project in ASP.Net Web API but having trouble pulling through the data from the Model to the Controller. I have tried a few approaches so far and read the tutorials from http://www.asp.net/web-api, but the table data, which is pulled through fine to the model does not make it to the controller for endpoint access. Is there a specific approach needed for these operations as the mock data used in the tutorials worked fine with the same structure?

Interface:

public interface ISkillRepository
{
    IEnumerable<Skill> GetAll();
    Skill Get(int id);
    Skill Add(Skill item);
    void Remove(int id);
    bool Update(Skill item);
}

Model:

public IEnumerable<Skill> GetAll()
    {
        using (var db = new XXXEntities())
        {
            var skills = db.Skill.ToList();
            return skills;
        }
    }

Controller:

static readonly ISkillRepository repository = new SkillsRepository();

    public IEnumerable<Skill> GetAllSkill()
    {
        return repository.GetAll();
    }

The model makes a successful call to the database and retrieves the information, however it does not pass successfully to the controller, which is being accessed as an endpoint. Pulling the model code into the controller also executes successfully but still throws a 500 when requested. There are no errors about incorrect return type etc. Any help would be much appreciated.

Moustachio
  • 184
  • 2
  • 23
  • What's the exact problem? Are you getting an error and if so what is it? Have you set a break point and stepped through the code? – Ciarán Bruen Jun 17 '12 at 11:11
  • Yes I have set breakpoints which is why I am scratching my head, even with the DB fetch code in the controller (debug shows correct data at this call), it still does not give a success when called. – Moustachio Jun 18 '12 at 00:19
  • Turns out this was a serializer issue, once I fix this I'll post the code. – Moustachio Jun 18 '12 at 00:49

1 Answers1

0

The solution as per EF 4.1 - Code First - JSON Circular Reference Serialization Error is to remove the 'virtual' type from the relations:

public /*virtual*/ ICollection<Skill> Skill { get; set; }

or

return accessibility + (accessibility != "private" ? "" : "");

rather than:

return accessibility + (accessibility != "private" ? " virtual " : "");

If using a model generator.

Community
  • 1
  • 1
Moustachio
  • 184
  • 2
  • 23