P粉7624473632023-08-18 09:50:10
You need to tell the .NET framework about the endpoint parameters and use the correct HTTP method to make the call. For example, changing the code slightly, you can use the Id parameter of the UserDetails method as a URL segment:
[HttpPost("{Id}"] public IEnumerable<user> UserDetails(string Id) { <code goes here> }The
HttpPost attribute tells the .NET Framework that this is a POST endpoint. Note the {Id} part in the HttpPost attribute. It's a placeholder and you should replace it with the actual ID when calling the endpoint. To call this endpoint, you need to send an HTTP POST request to the "users/123" endpoint, where the "123" part is a replacement for the {Id} placeholder. Here is an example:
fetch('users/123', { method: "POST" })
Endpoint URLs can also contain query string parameters. For example, here's how to call the UserInfo endpoint using query string parameters:
[HttpPost("{Id}"] public IEnumerable<user> UserInfo(string Id, [FromQuery] string name) { <code goes here> }
Note that we added the FromQuery attribute to the "name" parameter of the UserInfo method. The FromQuery attribute tells the .NET Framework that the URL should contain a query string parameter named name. Your endpoint URL will become "users/123?name=john", where "123" is the replacement of the {Id} placeholder and name=john is the assignment of the "name" query string parameter for "john".