Click here to Skip to main content
15,917,568 members
Home / Discussions / Web Development
   

Web Development

 
GeneralRe: ASP.Net MVC Core API Question Revisited Pin
Kevin Marois21-Apr-24 12:22
professionalKevin Marois21-Apr-24 12:22 
GeneralRe: ASP.Net MVC Core API Question Revisited Pin
Richard Deeming29-Apr-24 23:53
mveRichard Deeming29-Apr-24 23:53 
QuestionASP.Net MVC Core API Question Pin
Kevin Marois17-Apr-24 20:06
professionalKevin Marois17-Apr-24 20:06 
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);
        }
    }

    return results;
}
This works. It calls the GetAll
https: //localhost:5001/api/User
This works when calling GetById
https: //localhost:5001/api/User/GetById/1
This does NOT work. I get a not found
https: //localhost:5001/api/User/Login/jsmith/mypass
Can someone tell me what's wrong?
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.

AnswerRe: ASP.Net MVC Core API Question Pin
Pete O'Hanlon17-Apr-24 20:34
mvePete O'Hanlon17-Apr-24 20:34 
GeneralRe: ASP.Net MVC Core API Question Pin
Kevin Marois18-Apr-24 11:54
professionalKevin Marois18-Apr-24 11:54 
SuggestionRe: ASP.Net MVC Core API Question Pin
Richard Deeming18-Apr-24 0:06
mveRichard Deeming18-Apr-24 0:06 
QuestionWhat is generally the current best method for storing uploaded documents? Pin
we5inelgr6-Mar-24 15:06
we5inelgr6-Mar-24 15:06 
AnswerRe: What is generally the current best method for storing uploaded documents? Pin
Bohdan Stupak7-Mar-24 5:45
professionalBohdan Stupak7-Mar-24 5:45 
GeneralRe: What is generally the current best method for storing uploaded documents? Pin
we5inelgr7-Mar-24 9:05
we5inelgr7-Mar-24 9:05 
GeneralRe: What is generally the current best method for storing uploaded documents? Pin
jschell8-Mar-24 11:40
jschell8-Mar-24 11:40 
AnswerRe: What is generally the current best method for storing uploaded documents? Pin
jschell8-Mar-24 11:47
jschell8-Mar-24 11:47 
AnswerRe: What is generally the current best method for storing uploaded documents? Pin
Andre Oosthuizen9-Mar-24 0:47
mveAndre Oosthuizen9-Mar-24 0:47 
QuestionLooking for a working working sample google maps on blazor web app with loading markers from database Pin
urx194124-Jan-24 23:19
urx194124-Jan-24 23:19 
AnswerRe: Looking for a working working sample google maps on blazor web app with loading markers from database Pin
jschell25-Jan-24 4:36
jschell25-Jan-24 4:36 
GeneralRe: Looking for a working working sample google maps on blazor web app with loading markers from database Pin
michael floeter25-Jan-24 21:29
michael floeter25-Jan-24 21:29 
GeneralRe: Looking for a working working sample google maps on blazor web app with loading markers from database Pin
jschell30-Jan-24 4:51
jschell30-Jan-24 4:51 
QuestionSetting a CORS header, and allowing my Javascript modules that are inlined Pin
jkirkerx24-Jan-24 10:30
professionaljkirkerx24-Jan-24 10:30 
AnswerCleaned up my dart board, and got the modules error cleared at least, this will take time, not easy Pin
jkirkerx24-Jan-24 11:26
professionaljkirkerx24-Jan-24 11:26 
GeneralRe: The problem is somewhere else Pin
jkirkerx24-Jan-24 13:06
professionaljkirkerx24-Jan-24 13:06 
AnswerRe: Solved, that was a can of worms to sort out Pin
jkirkerx25-Jan-24 12:13
professionaljkirkerx25-Jan-24 12:13 
QuestionJS Arrow functions this & setTimeout scope Pin
Member 1618344416-Jan-24 1:46
Member 1618344416-Jan-24 1:46 
AnswerRe: JS Arrow functions this & setTimeout scope Pin
Jeremy Falcon25-Jan-24 4:31
professionalJeremy Falcon25-Jan-24 4:31 
QuestionWordPress for Windows Pin
Richard Andrew x647-Jan-24 4:24
professionalRichard Andrew x647-Jan-24 4:24 
AnswerRe: WordPress for Windows Pin
jschell8-Jan-24 5:52
jschell8-Jan-24 5:52 
GeneralRe: WordPress for Windows Pin
Richard Andrew x648-Jan-24 13:49
professionalRichard Andrew x648-Jan-24 13:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.