Click here to Skip to main content
15,899,126 members
Home / Discussions / Web Development
   

Web Development

 
PinnedHow to get an answer to your question Pin
Chris Maunder4-Sep-10 2:25
cofounderChris Maunder4-Sep-10 2:25 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder12-Jul-09 22:40
cofounderChris Maunder12-Jul-09 22:40 
QuestionConfiguration Error Pin
Richard Andrew x6411hrs 40mins ago
professionalRichard Andrew x6411hrs 40mins ago 
AnswerRe: Configuration Error Pin
Richard Deeming29mins ago
mveRichard Deeming29mins ago 
QuestionIdentity Management Recommendations Pin
Richard Andrew x6414-May-24 3:45
professionalRichard Andrew x6414-May-24 3:45 
AnswerRe: Identity Management Recommendations Pin
Richard Deeming14-May-24 3:51
mveRichard Deeming14-May-24 3:51 
GeneralRe: Identity Management Recommendations Pin
Richard Andrew x6414-May-24 4:32
professionalRichard Andrew x6414-May-24 4:32 
GeneralRe: Identity Management Recommendations Pin
Richard Deeming14-May-24 4:38
mveRichard Deeming14-May-24 4:38 
GeneralRe: Identity Management Recommendations Pin
Richard Andrew x6414-May-24 4:52
professionalRichard Andrew x6414-May-24 4:52 
QuestionGemfile.lock parsing/linting Pin
James Curran8-May-24 11:17
James Curran8-May-24 11:17 
AnswerRe: Gemfile.lock parsing/linting Pin
Richard Deeming8-May-24 21:32
mveRichard Deeming8-May-24 21:32 
QuestionSwagger Generated URL Questtion Pin
Kevin Marois26-Apr-24 12:37
professionalKevin Marois26-Apr-24 12:37 
AnswerRe: Swagger Generated URL Questtion Pin
Dave Kreskowiak26-Apr-24 12:55
mveDave Kreskowiak26-Apr-24 12:55 
GeneralRe: Swagger Generated URL Questtion Pin
Kevin Marois26-Apr-24 13:37
professionalKevin Marois26-Apr-24 13:37 
GeneralRe: Swagger Generated URL Questtion Pin
Dave Kreskowiak26-Apr-24 14:13
mveDave Kreskowiak26-Apr-24 14:13 
GeneralRe: Swagger Generated URL Questtion Pin
Kevin Marois26-Apr-24 14:17
professionalKevin Marois26-Apr-24 14:17 
QuestionASP.Net MVC Core API Question Revisited Pin
Kevin Marois18-Apr-24 19:44
professionalKevin Marois18-Apr-24 19:44 
AnswerRe: ASP.Net MVC Core API Question Revisited Pin
Richard Deeming18-Apr-24 21:27
mveRichard Deeming18-Apr-24 21:27 
GeneralRe: ASP.Net MVC Core API Question Revisited Pin
Kevin Marois19-Apr-24 4:20
professionalKevin Marois19-Apr-24 4:20 
GeneralRe: ASP.Net MVC Core API Question Revisited Pin
Richard Deeming19-Apr-24 4:43
mveRichard Deeming19-Apr-24 4:43 
GeneralRe: ASP.Net MVC Core API Question Revisited Pin
Kevin Marois19-Apr-24 5:19
professionalKevin Marois19-Apr-24 5:19 
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 

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.