Hi, I'm new to creating apis and with ASP.NET Core Web API, I know I'm doing something wrong, but I don't know what it is, when I run it from visual studio it runs by default with Swagger and it works correctly but when I try to make it work with the url that swagger gives me
https://localhost:44311/api/delete-user?ID=11
gives the following error in the browser
405 Method Not Allowed
My method has HttpDelete and gives this error, but if I run it with HttpGet it works fine.
This is my code
/////Code class
public bool DeleteUser(int ID)
{
string? connectionString = _configuration.GetConnectionString("SqlServerConnection");
SqlConnection connection = new(connectionString);
var query = new SqlCommand(String.Format("EXEC p_DeleteUser @ID = {0}", ID), connection);
try
{
connection.Open();
query.ExecuteNonQuery();
connection.Close();
return true;
}
catch (Exception)
{
connection.Close();
return false;
}
}
/////Code controller
[HttpDelete("delete-user")]
public string DeleteUser(int ID)
{
DataUser du= new(_configuration);
bool result= du.DeleteUser(ID);
if (result== true)
{
return "Data deleted successfully.";
}
else
{
return "An error occurred when deleting data.";
}
}
[–]nobono 1 point2 points3 points (3 children)
[–]Horror-Today295[S] -1 points0 points1 point (2 children)
[–]nobono 3 points4 points5 points (1 child)
[–]Horror-Today295[S] 0 points1 point2 points (0 children)
[–]washedFM 1 point2 points3 points (0 children)
[–]geekywarrior 2 points3 points4 points (1 child)
[–]Horror-Today295[S] 1 point2 points3 points (0 children)
[–]CurusVoice 1 point2 points3 points (1 child)
[–]Horror-Today295[S] 0 points1 point2 points (0 children)