I have three function in .Net web api in .Net 6.
First:
[HttpPost("name/{name}")]
public async Task<ActionResult<Book>> PostOrder(string name, Book book)
{
try
{
var id = _context.Order.Where(el => el.User == name).Select(el => el.OrderID).FirstOrDefault();
if (id > 0)
{
book.OrderID = id;
_context.Book.Update(book);
await _context.SaveChangesAsync();
return book;
}
else
{
Order o = new()
{
User = name
};
_context.Order.Add(o);
await _context.SaveChangesAsync();
id = _context.Order.Where(el => el.User == name).Select(el => el.OrderID).FirstOrDefault();
book.OrderID = id;
_context.Book.Update(book);
await _context.SaveChangesAsync();
return book;
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return NotFound();
}
}
Second and third
[HttpPut("{id}")]
public async Task<IActionResult> PutUser(int id, User user)
{
if (id != user.id)
{
return BadRequest();
}
_context.Entry(user).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!UserExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
// POST: api/Users
[HttpPost]
public async Task<ActionResult<User>> PostUser(User user)
{
_context.User.Add(user);
await _context.SaveChangesAsync();
return CreatedAtAction("GetUser", new { id = user.id }, user);
}
And I use to communication Angular: First:
addToOrder(user: string, book: Book): Observable<Book> {
const url = `${this.orderUrl}/name/${user}`;
return this.http.post<Book>(this.orderUrl, book, this.httpOptions).pipe(
tap(_ => this.log(`added book to order for ${user}`)),
catchError(this.handleError<Book>('addToOrder'))
);
}
Second and third:
updateUser(user: User): Observable<any> {
const url = `${this.usersUrl}/${user.id}`;
return this.http.put(url, user, this.httpOptions).pipe(
tap(_ => this.log(`updated users id=${user.id}`)),
catchError(this.handleError<any>('updateUser'))
);
}
addUser(user: User): Observable<User> {
return this.http.post<User>(this.usersUrl, user, this.httpOptions).pipe(
tap((newUser: User) => this.log(`added user w/ id=${newUser.id}`)),
catchError(this.handleError<User>('addUser'))
);
}
And first no working. No communicating with .Net, where is wrong data? I can't find any. Mayby parameters is incorect. But I do not see. Any other function working, I have responde and in debug the server is requested but on first request no. Only in POSTMAN or SWAGGER.
I have in orders on Angular:
getOrder(user: string): Observable<Book[]> {
const url = `${this.orderUrl}/name/${user}`;
return this.http.get<Book[]>(url)
.pipe(
tap(_ => this.log(`fetched order for name=${user}`)),
catchError(this.handleError<Book[]>('getBooks', []))
);
}
Is working to.
When I try to use Postman or Swagger I see responde.
P.S when I coppy address on debug from:
const url = `${this.orderUrl}/name/${user}`;
And try in postman added Book in JSON:
{
"bookID": 4,
"title": "string2",
"author": "strin2g",
"isbn": "stri2ng"
}
I have responde.
CodePudding user response:
By default POST method parameters must all be in the request body (FromBody
).
GET method parameters must all be in the request query (FromUri
).
To change this behavior, you need to add Parameters Bindings.
Changes
[HttpPost("name/{name}")]
public async Task<ActionResult<Book>> PostOrder(
[FromUri] string name,
[FromBody] Book book)
{
// CODE
}
CodePudding user response:
Ok. I have a answer. I havae a:
const url = `${this.orderUrl}/name/${user}`;
return this.http.post<Book>(this.orderUrl, book, this.httpOptions)
But must be:
const url = `${this.orderUrl}/name/${user}`;
return this.http.post<Book>(url, book, this.httpOptions)
Sory for problem. I didn't notice it.