locked
HTTP Error 404.0 - Not Found RRS feed

  • Question

  • User2100448650 posted

    Hi All

    I'm busy building my first WebApi, I have successfully connected to my local SQL database and can retrieve the list of all users from my users table. For some reason I get a "HTTP Error 404.0 - Not Found" when trying to pull I single user with the users ID. I have checked my database and the users ID I am using is definitely there but for some reason I cannot get it to work. Below is the code samples for my "GetAllUsers" call which works fine and two different snippets of "GetUser" I've been testing with to retrieve a user from their id does not work at all. Please can someone tell me what I'm doing wrong here.

    namespace ImpactWebApi.Models
    {
        public class ImpactController : ApiController
    
        {
            public IEnumerable<ImpactUsersModel.UsersModel> GetAllUsers()
            {
                List<ImpactUsersModel.UsersModel> Lists = new List<ImpactUsersModel.UsersModel>();
                using (ImpactWebApiEntities entities = new ImpactWebApiEntities())
                {
                    var result = entities.Users.ToList();
    
                    foreach (User u in result)
                    {
                        Lists.Add(new ImpactUsersModel.UsersModel { Id = u.User_No, Name = u.User_Name });
                    }
    
                    return Lists;
                }
            }
    
            public User GetUser(int UserId)
            {
                using (ImpactWebApiEntities entities = new ImpactWebApiEntities())
                {
                    return entities.Users.FirstOrDefault(e => e.User_No == UserId);
                }
    
            }
    
    
            public IHttpActionResult GetUser(int UserId)
            {
                using (ImpactWebApiEntities entities = new ImpactWebApiEntities())
                {
                    var User = entities.Users.FirstOrDefault(e => e.User_No == UserId);
                    return Ok(new ImpactUsersModel.UsersModel { Id = User.User_No, Name = User.User_Name });
                }
            }
    
        }
    }

    I'm using the following URL to try get the user with the ID of "0"

    http://localhost:50696/Api/Impact/GetUser/0

    many thanks in advance.

    Monday, January 29, 2018 8:36 PM

Answers

  • User2100448650 posted

    I finally found the problem, it was with my URL. it should have been "Api/Impact/0" to return a single user and "Api/Impact/" for all users.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 30, 2018 8:40 PM

All replies

  • User475983607 posted

    The default route uses "id" not "UserId"  Did you update the routes?  If not change the action methods parameters to id.

            public IHttpActionResult GetUser(int id)
            {
                using (ImpactWebApiEntities entities = new ImpactWebApiEntities())
                {
                    var User = entities.Users.FirstOrDefault(e => e.User_No == id);
                    return Ok(new ImpactUsersModel.UsersModel { Id = User.User_No, Name = User.User_Name });
                }
            }

    Reference docs

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    Monday, January 29, 2018 11:23 PM
  • User2100448650 posted

    Hi mgebhard

    Thank you for your reply, I've changed it back as per your suggestion, I didn't change the routes so that was one issue. I still get the same "error 404" message when calling "GetUser" from the URL. I've read through the link you provided and everything seems correct but I just cannot get this to work. Anything else I could try ? It doesn't make sense why I can retrieve all users but not a single user.

    namespace ImpactWebApi
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // Web API configuration and services
    
    
                // Web API routes
                config.MapHttpAttributeRoutes();
    
                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{id}",
                    defaults: new { id = RouteParameter.Optional }
                );
    
            }
        }
    }
            public IHttpActionResult GetUser(int id)
            {
                using (ImpactWebApiEntities entities = new ImpactWebApiEntities())
                {
                    var User = entities.Users.FirstOrDefault(e => e.User_No == id);
                    return Ok(new ImpactUsersModel.UsersModel { id = User.User_No, Name = User.User_Name });
                }
            }

    Thanks again for any advice or help.

    Tuesday, January 30, 2018 8:22 PM
  • User475983607 posted

    I just noticed you have two GetUser methods.  The code should not compile.  Can you post the actual api controller.

    Also your controller is in the Model namespace.  

    Perhaps start over...

    Tuesday, January 30, 2018 8:38 PM
  • User2100448650 posted

    I finally found the problem, it was with my URL. it should have been "Api/Impact/0" to return a single user and "Api/Impact/" for all users.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 30, 2018 8:40 PM