The action input is incorrect. Also you named the method Put but you used the HttpPost attribute. You are sending a POST correct? Anyway, pass a single type.
[HttpPost]
[Route("AddTestEvent")]
public async Task<IActionResult> PutTestEvent(string id, string testid)
Create a ViewModel
public class TestViewModel
{
public string ID {get; set;}
public string TestID {get; set;}
}
Use the ViewModel as the input parameter.
[HttpPost]
[Route("AddTestEvent")]
public async Task<IActionResult> PostTestEvent(TestViewModel model)
Anyway, Web API programming patterns are covered in the official Web API documentation. I recommenced reading the docs rather than using trial and error.
Thanks for the response and helpful advice. Unfortunately I already read that doc and got the example working, and also tried the exact call you suggested (i noted that in my original post, around line 11 or 12), but for some reason after getting the example
working when I tried to replicate those methods in my code it did not work. That's why I posted the question
Thanks for the response and helpful advice. Unfortunately I already read that doc and got the example working, and also tried the exact call you suggested (i noted that in my original post, around line 11 or 12), but for some reason after getting the example
working when I tried to replicate those methods in my code it did not work. That's why I posted the question
I do not understand your response. Below is a working example.
[Route("api/[controller]")]
[ApiController]
public class DefaultController : ControllerBase
{
// POST: api/Default
[HttpPost]
[Route("AddTestEvent")]
public TestViewModel Post([FromBody] TestViewModel value)
{
return value;
}
Member
6 Points
32 Posts
ASP.NET Core Web API?
Dec 30, 2019 12:04 PM|john.straumann|LINK
Hi all:
I am trying to create a simple HTTPPOST method to update a database. With the Code pasted below, the only way the call works is:
https://localhost:44309/api/TestEvent/AddTestEvent?id=4518e481-9965-4b6c-9916-f4c15vbg000&testid=DFCVBR
but I'd like the call to be:
https://localhost:44309/api/TestEvent/AddTestEvent
with the inputs as JSON:
{
"ID": "4518e481-9965-4b6c-9916-f4c15vbg000",
"TestID": "DFCVBR"
}
I tried defining the method as:
public async Task<IActionResult> PutTestEvent(TestEvent testevent)
but when I try to call that from PostMan with the JSON as the body, I get a "method not allowed" error.
Thanks for any and all input!
John.
[HttpPost]
[Route("AddTestEvent")]
public async Task<IActionResult> PutTestEvent(string id, string testid)
{
TestEvent testevent = new TestEvent();
testevent.ID = id;
testevent.TestID = testid;
_context.TestEvent.Add(testevent);
try
{
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
if (ex.GetType().FullName ==
"Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException")
{
Console.WriteLine(ex.ToString());
return NotFound();
}
Console.WriteLine(ex.ToString());
return BadRequest();
}
return NoContent();
}
All-Star
53041 Points
23612 Posts
Re: ASP.NET Core Web API?
Dec 30, 2019 04:45 PM|mgebhard|LINK
The action input is incorrect. Also you named the method Put but you used the HttpPost attribute. You are sending a POST correct? Anyway, pass a single type.
[HttpPost] [Route("AddTestEvent")] public async Task<IActionResult> PutTestEvent(string id, string testid)
Create a ViewModel
Use the ViewModel as the input parameter.
Anyway, Web API programming patterns are covered in the official Web API documentation. I recommenced reading the docs rather than using trial and error.
https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.1
Member
6 Points
32 Posts
Re: ASP.NET Core Web API?
Dec 30, 2019 05:40 PM|john.straumann|LINK
All-Star
53041 Points
23612 Posts
Re: ASP.NET Core Web API?
Dec 30, 2019 06:59 PM|mgebhard|LINK
I do not understand your response. Below is a working example.
URL
Model
Member
6 Points
32 Posts
Re: ASP.NET Core Web API?
Dec 30, 2019 07:34 PM|john.straumann|LINK
Thanks, got it!