1

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);
        }

    }
}
3
  • 1
    Have you looked at any web api examples? Because the use of the View method is not a part of web api. Commented Oct 17, 2014 at 17:19
  • 3
    WebApi shouldnt be returning a view, you probably want to just return objectmodel, or some IEnumerable<InfoModel> like your method signature Commented Oct 17, 2014 at 17:19
  • To elaborate on why you don't return view, there is no UI to a Web API. Whereas is model-VIEW-controller, the intent of Web API is to be consumed directly by other code, so it's only necessary to return the data, and not a view. Commented Oct 29, 2014 at 18:52

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I just try your routine but I get the error "The name 'item' does not exist in this context". Any suggestions?
It worked like perfectly. I finally have an example that is working completely and I hope to build off that. Thank you for your time.
0

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...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.