Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.Net MVC Core API Question

ASP.Net MVC Core API Question

Scheduled Pinned Locked Moved Web Development
asp-netcsharpsysadminjsonarchitecture
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Kevin Marois
    wrote on last edited by
    #1

    I'm trying to learn ASP.Net MVC Core API. I think I'm doing the routing wronge. I have a UserController:

    namespace Falcon.API.Controllers
    {
    [Route("api/user")]
    [ApiController]
    public class UserController : _ControllerBase
    {
    public UserController(IConfiguration configuration) :
    base(configuration)
    {
    }

        \[HttpGet("getById/{id}")\]
        public IActionResult GetById(int id)
        {
            try
            {
                var repo = new Repository(GetDataContext());
    
                var owner = repo.GetById(id);
    
                if (owner is null)
                {
                    return NotFound();
                }
                else
                {
                    return Ok(owner);
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }
    
        \[HttpGet\]
        public IActionResult GetAll()
        {
            try
            {
                var repo = new Repository(GetDataContext());
    
                var owners = repo.GetAll();
    
                return Ok(owners);
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }
    
        \[HttpGet("login/{username}/{password}")\]
        public IActionResult Login(string userName, string password)
        {
            try
            {
                var repo = new UserRepository(GetDataContext());
    
                var owner = repo.Login(userName, password);
    
                if (owner is null)
                {
                    return NotFound();
                }
                else
                {
                    return Ok(owner);
                }
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Internal server error");
            }
        }
    }
    

    }

    When I call it, I'm doing this:

    public async Task Login(string userName, string password)
    {
    UserEntity results = null;

    var url = $"https:// localhost:5001/api/User/Login/{userName}/{password}";
    
    using (var httpClient = new HttpClient())
    {
        using (var response = await httpClient.GetAsync(url))
        {
            string apiResponse = await response.Content.ReadAsStringAsync();
            results = JsonConvert.DeserializeObject(apiResponse)
    
    P R 2 Replies Last reply
    0
    • K Kevin Marois

      I'm trying to learn ASP.Net MVC Core API. I think I'm doing the routing wronge. I have a UserController:

      namespace Falcon.API.Controllers
      {
      [Route("api/user")]
      [ApiController]
      public class UserController : _ControllerBase
      {
      public UserController(IConfiguration configuration) :
      base(configuration)
      {
      }

          \[HttpGet("getById/{id}")\]
          public IActionResult GetById(int id)
          {
              try
              {
                  var repo = new Repository(GetDataContext());
      
                  var owner = repo.GetById(id);
      
                  if (owner is null)
                  {
                      return NotFound();
                  }
                  else
                  {
                      return Ok(owner);
                  }
              }
              catch (Exception ex)
              {
                  return StatusCode(500, "Internal server error");
              }
          }
      
          \[HttpGet\]
          public IActionResult GetAll()
          {
              try
              {
                  var repo = new Repository(GetDataContext());
      
                  var owners = repo.GetAll();
      
                  return Ok(owners);
              }
              catch (Exception ex)
              {
                  return StatusCode(500, "Internal server error");
              }
          }
      
          \[HttpGet("login/{username}/{password}")\]
          public IActionResult Login(string userName, string password)
          {
              try
              {
                  var repo = new UserRepository(GetDataContext());
      
                  var owner = repo.Login(userName, password);
      
                  if (owner is null)
                  {
                      return NotFound();
                  }
                  else
                  {
                      return Ok(owner);
                  }
              }
              catch (Exception ex)
              {
                  return StatusCode(500, "Internal server error");
              }
          }
      }
      

      }

      When I call it, I'm doing this:

      public async Task Login(string userName, string password)
      {
      UserEntity results = null;

      var url = $"https:// localhost:5001/api/User/Login/{userName}/{password}";
      
      using (var httpClient = new HttpClient())
      {
          using (var response = await httpClient.GetAsync(url))
          {
              string apiResponse = await response.Content.ReadAsStringAsync();
              results = JsonConvert.DeserializeObject(apiResponse)
      
      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      When you step through the Login code, what happens? The problem has to lie inside your repo.Login method, so that's the place you should be looking.

      Advanced TypeScript Programming Projects

      K 1 Reply Last reply
      0
      • K Kevin Marois

        I'm trying to learn ASP.Net MVC Core API. I think I'm doing the routing wronge. I have a UserController:

        namespace Falcon.API.Controllers
        {
        [Route("api/user")]
        [ApiController]
        public class UserController : _ControllerBase
        {
        public UserController(IConfiguration configuration) :
        base(configuration)
        {
        }

            \[HttpGet("getById/{id}")\]
            public IActionResult GetById(int id)
            {
                try
                {
                    var repo = new Repository(GetDataContext());
        
                    var owner = repo.GetById(id);
        
                    if (owner is null)
                    {
                        return NotFound();
                    }
                    else
                    {
                        return Ok(owner);
                    }
                }
                catch (Exception ex)
                {
                    return StatusCode(500, "Internal server error");
                }
            }
        
            \[HttpGet\]
            public IActionResult GetAll()
            {
                try
                {
                    var repo = new Repository(GetDataContext());
        
                    var owners = repo.GetAll();
        
                    return Ok(owners);
                }
                catch (Exception ex)
                {
                    return StatusCode(500, "Internal server error");
                }
            }
        
            \[HttpGet("login/{username}/{password}")\]
            public IActionResult Login(string userName, string password)
            {
                try
                {
                    var repo = new UserRepository(GetDataContext());
        
                    var owner = repo.Login(userName, password);
        
                    if (owner is null)
                    {
                        return NotFound();
                    }
                    else
                    {
                        return Ok(owner);
                    }
                }
                catch (Exception ex)
                {
                    return StatusCode(500, "Internal server error");
                }
            }
        }
        

        }

        When I call it, I'm doing this:

        public async Task Login(string userName, string password)
        {
        UserEntity results = null;

        var url = $"https:// localhost:5001/api/User/Login/{userName}/{password}";
        
        using (var httpClient = new HttpClient())
        {
            using (var response = await httpClient.GetAsync(url))
            {
                string apiResponse = await response.Content.ReadAsStringAsync();
                results = JsonConvert.DeserializeObject(apiResponse)
        
        R Offline
        R Offline
        Richard Deeming
        wrote on last edited by
        #3

        Passing the credentials in the URL of a GET request is a very bad idea. You should only ever use a POST request. With a GET request, you will end up with the credentials stored in plain-text in every log between you and the user, and in the browser history.


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

        1 Reply Last reply
        0
        • P Pete OHanlon

          When you step through the Login code, what happens? The problem has to lie inside your repo.Login method, so that's the place you should be looking.

          Advanced TypeScript Programming Projects

          K Offline
          K Offline
          Kevin Marois
          wrote on last edited by
          #4

          I guess what I'm asking is - isn't this the wrong way to pass params?

          var url = $"https:// localhost:5001/api/User/Login/{userName}/{password}";

          If so, that means I've set something up incorrectly. But I don't really know what.

          In theory, theory and practice are the same. But in practice, they never are.” If it's not broken, fix it until it is. Everything makes sense in someone's mind.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups