Robert Messina
Robert Messina

Reputation: 13

Web API won't return JSON using stored procedure

I'm new to coding WEB APIs (using VS 2013) and I have tried to get a stored procedure to return results in JSON. I have found examples using HTML for display, but I can't get it send JSON. I'm working with that example and the error I'm seeing is "The name 'view' does not exist in the current context". I usually work though problems pretty well using the message boards (and I feel I'm close) but I just can't seem to get this to succeed. The data is there from the sproc but I don't know how to return it. This is my first post so forgive me if the answer turns out to be obvious.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TRYiT.Models;

namespace TRYiT.Controllers
{
    public class TRYiTController : ApiController
    {
        TRYiTEntities _db = new TRYiTEntities();
        public IEnumerable<InfoModel> Get()
        {
            var studentercord = _db.Student_sp().ToList();
            InfoModel objmodel = new InfoModel();
            objmodel.infoData = new List<info>();
            foreach (var item in studentercord.ToList())
            {
                objmodel.infoData.Add(new info { StudentID = item.StudentID, 
                                                 LastName = item.LastName, 
                                                 FirstName = item.FirstName, 
                                                 EnrollmentDate = item.EnrollmentDate, 
                                                 MiddleName = item.MiddleName });
            }
            return view(objmodel);
        }

    }
}

Upvotes: 1

Views: 3829

Answers (2)

Francis Ducharme
Francis Ducharme

Reputation: 4987

I'd rather do something like

public HttpResponseMessage Get()
{
    var studentercord = _db.Student_sp().ToList();
    InfoModel objmodel = new InfoModel();
    objmodel.infoData = new List<info>();
    foreach (var item in studentercord.ToList())
    {
        objmodel.infoData.Add(new info { StudentID = item.StudentID, 
                                         LastName = item.LastName, 
                                         FirstName = item.FirstName, 
                                         EnrollmentDate = item.EnrollmentDate, 
                                         MiddleName = item.MiddleName });
    }

    return Request.CreateResponse(HttpStatusCode.OK, objmodel.infoData);
    //or return Request.CreateResponse(HttpStatusCode.OK, objmodel);
    //depending what your client expects...
}

Upvotes: 0

Arindam Nayak
Arindam Nayak

Reputation: 7462

I hope, you are looking for this.

public IEnumerable<info> Get()
{
    var studentercord = _db.Student_sp().ToList();
    IEnumerable<info> data = (from item in studentercord
                                select new info
                                {
                                    StudentID = item.StudentID,
                                    LastName = item.LastName,
                                    FirstName = item.FirstName,
                                    EnrollmentDate = item.EnrollmentDate,
                                    MiddleName = item.MiddleName
                                }).ToList();

    return data;
}

NOTE: I have changed return type to info, because i can't see that IEnumerable<InfoModel> can not be returned from here, rather it makes sense to use IEnumerable<info> . So if you feel it is correct ,go ahead and make changes.

Becaue, as you can see that return type is of IEnumerable<T> , so you do not have to return View . In webapi, you need to return IEnumerable, so that when you execute Get request, you will get list of data ( for this example). But for MVC, you have to return View while invoking controller , whereas WebApi return type is not like that.

So assuming, this is hosted in "http://www.example.com" and has route defined as "api/{controller}", you need to hit "http://www.example.com/api/TRYiT" to get list of data.

Upvotes: 2

Related Questions