1

I have an issue and I got no idea what to do, the issue is that on my C# WebApi app with swagger enabled.

i have a few apis but here is an example 1 of them.

[HttpPost]
[Route("/api/user/register")]
public UserSession Register(string email, string password, string confirm_password)
{
    if (password != confirm_password)
    {
        return new UserSession()
        {
            Success = false,
            Message = "Error Passwords don't match",
            SessionKey = "",
        };
    }
    // success code here
}             

here is the angular API.

import axios from 'axios';

export class API {
    private static base_api:string = "http://localhost:51019/";
    static register(email:string, password:any, confirm_password:any) {
        let url = this.base_api + "api/user/register";
        let data = {
            email: email,
            password: password,
           confirm_password: confirm_password,
        };    
        const headers = {
            'Content-Type': 'application/json'
        };
        let result = axios.post(url, data, {headers}).then(x=>{return x}).catch(x=>{return false;});
        console.log(result);
    }
}

even when I provide an email and/or a password, it's like the API isn't receiving the data?

to fix the cor issue i had i added this to the api controller

[HttpOptions]
[Route("/api/user/register")]
[Route("/api/user/login")]
[Route("/api/user/logout")]
public HttpResponseMessage Options()
{
    var response = new HttpResponseMessage();
    response.StatusCode = HttpStatusCode.OK;
    return response;
}

if i access the api via the swagger ui via (http://localhost:51019/swagger/index.html)

then when i perform the api via the UI it works correctly.

TIA

1 Answer 1

2

Typically you'd create a model and set it as the body:

public sealed class RegisterModel
{
    [JsonPropertyName("email")]
    public string Email { get; set; }

    [JsonPropertyName("password")]
    public string Password { get; set; }

    [JsonPropertyName("confirm_password")]
    public string ConfirmPassword { get; set; }
}

[HttpPost, Route("/api/user/register")]
public UserSession Register([FromBody] RegisterModel register)
{
    // ...
}

That will get it to work in all scenarios

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.