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'.
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:
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);
}