Home > Mobile >  How to use correctly use ASP.Net Web API to get and display SQL table?
How to use correctly use ASP.Net Web API to get and display SQL table?

Time:07-06

I'm a beginner to ASP.Net and was given a project to try and create an ASP.Net Web Form that calls an ASP.Net Web API that grabs data from an SQL Server. I have no experience working with any of these things and am trying to go through this step by step. I am supposed to do this using only ADO.Net, although I'm not sure I fully understand how to do that yet.

Currently, my API has a model written as

namespace MovieAPI.Models
{
    using System;
    using System.Collections.Generic;
    
    public partial class tblMovie
    {
        public int MovieID { get; set; }
        public string MovieTitle { get; set; }
        public string MovieRating { get; set; }
        public int ReleaseYear { get; set; }
    }
}

(This Model code was auto-generated after connecting to the SQL Server via ADO.Net Entity Data Model)

and a controller written as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using MovieAPI.Models;
using System.Data;
using System.Data.SqlClient;

namespace MovieAPI.Controllers
{
    public class MovieController : ApiController
    {

        public IHttpActionResult getMovieDetails()
        {
            MoviesDBEntities entities = new MoviesDBEntities();
            var results = entities.tblMovies.ToList();
            return Ok(results);
        }

    }
}

it is my hope that when I press the run button that I would be able to go to my URL localhost:.../api/MovieController to see the full table grabbed from the SQL server. However, when I attempt this I get the following error:

<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost:58749/api/MovieController'.</Message>
<MessageDetail>No type was found that matches the controller named 'MovieController'.</MessageDetail>
</Error>

Obviously, something is going wrong here but I'm not quite sure what. I've tried to look up many tutorials but the implementations and versions can be radically different to the point where I get lost.

I've seen some people talk about a Web.config file and messing around with that, but I was unable to figure out what to do when looking at my own.

Any helpful pointers to get me through this? I've been banging my head against a wall for a couple days over this.

EDIT: Apparently the issue was the URL. Changing it from localhost.../api/MovieController to localhost.../api/Movie fixed it. I was able to see the entire table successfully. Thanks again CodingMytra!

CodePudding user response:

[HttpGet("GetDetails")]
public IHttpActionResult getMovieDetails()
{
 MoviesDBEntities entities = new MoviesDBEntities();
 var results = entities.tblMovies.ToList();
 return Ok(results);
}

http://localhost:58749/api/Movies/GetDetails

It can be somemore verbose like this to improve readability

CodePudding user response:

Apparently the issue was the URL. Changing it from localhost.../api/MovieController to localhost.../api/Movie fixed it. I was able to see the entire table successfully. Thanks again CodingMytra!

  • Related