0

So, I am learning how to create a Web Api in ASP.Net MVC 5. And I am stuck at one place and need guidance.

Steps Taken:

  • Created an API Folder in Controller Folder and then a file "ResumesController.cs" inside it.

ResumesController.cs

public class ResumesController : ApiController
{
    private ApplicationDbContext _context;

    public ResumesController()
    {
        _context = new ApplicationDbContext();
    }

    //GET /api/resumes
    public IEnumerable<Resume> GetResumes()
    {
        var candidateId = User.Identity.GetUserId();
        var resumeList = _context
            .Resumes.Where(r => r.CandidateId == candidateId)
            .OrderByDescending(r => r.datetime);

        return resumeList;
    }

}

Global.Asax

 protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

Url exceuted:

http://localhost:44395/api/resumes

Error Message:

The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.

enter image description here

Non Api Attempt: Works Fine

  public ActionResult ResumeCenter()
        {
            var candidateId = User.Identity.GetUserId();
            var resumeList = _context
                .Resumes.Where(r => r.CandidateId == candidateId)
                .OrderByDescending(r => r.datetime);

            return View(resumeList);
        }

Folder and File Structure:

enter image description here

Question 1: I am learning via a tutorial and I don't understand how come I need to call api/resumes? Wy don't I need to call something like /api/GetResumes?

Question 2: Why am I not able to see the XML view of data returned?

P:S: Breakpoint is getting hit in the API Controller Action.

Attempt 1:

 protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
        GlobalConfiguration.Configuration.Formatters
            .Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • WebApi has some routing smarts, eg URLs with a "Get" in the method name can be omitted, you can change this in the routing. As for your second question I found the duplicate - the solution is to return a Model (POCO) not an Entity Framework object, OR add some config to `GlobalConfiguration.Configuration`, or try one of the handful of other good answers. Let me know if it is NOT an **exact** duplicate. https://stackoverflow.com/questions/23098191/failed-to-serialize-the-response-in-web-api-with-json – Jeremy Thompson Oct 02 '17 at 01:35
  • @JeremyThompson: Can you kindly put the link that I need to look into? – Unbreakable Oct 02 '17 at 01:38
  • @JeremyThompson: Okey, I modified the Global.Asax and added some line of code and now it works. But I don't understand the tutorial I am watching does not add those lines still he is able to get the desired results? Wonder why? – Unbreakable Oct 02 '17 at 01:43
  • Also, the answer I attempted has 71 upvotes w/o any explanation. Can you kindly explain me why it works for me now. (See my Added Attempt). – Unbreakable Oct 02 '17 at 01:45
  • @JeremyThompson: Resume is my POCO. Its my Model. :-| – Unbreakable Oct 02 '17 at 01:51
  • I changed to basic `return _context.Resumes.ToList();` But this also does not work. – Unbreakable Oct 02 '17 at 02:01
  • Okey My Resume Model in turn uses some other Model. so, that might be the reason. :-| – Unbreakable Oct 02 '17 at 02:04
  • Perhaps score some rep by posting your solution in the duplicate, or even a comment! It'll most likely help someone else... In the future! – Jeremy Thompson Oct 02 '17 at 07:12

0 Answers0