如何在 Blazor 应用程序中从 Http.PutAsJsonAsync 传递多个字符串值
How to pass multiple string values from Http.PutAsJsonAsync in Blazor app
我需要 Post 两个字符串值才能在数据库中添加公司。这里我把 PUT 写成
await Http.PutAsJsonAsync($"api/B2CUsers/company/invitation/add/{UtpID}/{User.email}",UtpID,User.email);
我在 User.email
收到一个错误,显示 cannot convert string to System.Text.Json.JsonSerializerOptions
。
控制器功能:
[HttpPut("company/invitation/add/{utpid}/{emailAddressInvitedUser}")]
public async Task<int> AddUserCompanyInvitationAsync(string utpid, string emailAddressInvitedUser)
{
string userid = GetCurrentUser();
return await _iB2CUserServiceRepository.AddUserCompanyInvitation(userid, utpid, emailAddressInvitedUser);
}
注意:通常将单个值传递给 Web Api 端点,因此您最好定义一个包含两个字符串值的 DTO 对象,如下所示:
var dtoObj = new DtoObject { UtpID = UtpID, Email = User.email };
using var response =
await htpClient.PutAsJsonAsync("api/B2CUsers/company/invitation/add",
dtoObj );
response.EnsureSuccessStatusCode();
注意:您的代码应该定义一个名为 DtoObject 的对象,该对象的作用域是您的 Web Api 和 Blazor(也许您可以将它放在共享项目中?)
在 AddUserCompanyInvitationAsync Web Api 方法中,您将获得一个 DtoObject 类型的参数对象,您可以像这样在代码中使用它:
return await
_iB2CUserServiceRepository.AddUserCompanyInvitation(userid, dtoObj.UtpID , dtoObj.Email);
正确的函数签名是
[HttpGet("company/invitation/add2/{utpid}/{emailAddressInvitedUser}")]
public Task<int> AddUserCompanyInvitationAsync([FromRoute] string utpid, [FromRoute] string emailAddressInvitedUser)
如果您不添加 [FromRoute] 属性,.NET 将不会将路由值映射到您的变量。
请记住,此类请求将记录在网络服务器日志中。这意味着电子邮件地址将在日志中可见。您还需要使用 HttpUtility.UrlEncode
转义电子邮件字符串
curl -X PUT "http://localhost:14786/company/invitation/add2/45/test%40test.com" -H "accept: text/plain"
首选方法是在正文中传递 Json 请求。
public class AddUserRequest
{
public int utpid { get; set; }
public string emailAddressInvitedUser { get; set; }
}
public class AddUserResponse
{
public int uid { get; set; }
}
[HttpPut("company/invitation/add1")]
public Task<AddUserResponse> AddUserCompanyInvitationAsync([FromBody]AddUserRequest addUserRequest)
{
Console.WriteLine(addUserRequest.utpid);
Console.WriteLine(addUserRequest.emailAddressInvitedUser);
return Task.FromResult(new AddUserResponse() { uid = 1 });
}
并为客户
var client = new HttpClient();
int id = 45;
string email = "test@test.com";
await client.GetAsync($"http://localhost:14786/company/invitation/add2/{id}/{System.Web.HttpUtility.UrlEncode(email)}");
var response = await client.PutAsJsonAsync("http://localhost:14786/company/invitation/add1", new AddUserRequest() { utpid = id, emailAddressInvitedUser = email });
而不是 PUT 使用 POST
我需要 Post 两个字符串值才能在数据库中添加公司。这里我把 PUT 写成
await Http.PutAsJsonAsync($"api/B2CUsers/company/invitation/add/{UtpID}/{User.email}",UtpID,User.email);
我在 User.email
收到一个错误,显示 cannot convert string to System.Text.Json.JsonSerializerOptions
。
控制器功能:
[HttpPut("company/invitation/add/{utpid}/{emailAddressInvitedUser}")]
public async Task<int> AddUserCompanyInvitationAsync(string utpid, string emailAddressInvitedUser)
{
string userid = GetCurrentUser();
return await _iB2CUserServiceRepository.AddUserCompanyInvitation(userid, utpid, emailAddressInvitedUser);
}
注意:通常将单个值传递给 Web Api 端点,因此您最好定义一个包含两个字符串值的 DTO 对象,如下所示:
var dtoObj = new DtoObject { UtpID = UtpID, Email = User.email };
using var response =
await htpClient.PutAsJsonAsync("api/B2CUsers/company/invitation/add",
dtoObj );
response.EnsureSuccessStatusCode();
注意:您的代码应该定义一个名为 DtoObject 的对象,该对象的作用域是您的 Web Api 和 Blazor(也许您可以将它放在共享项目中?)
在 AddUserCompanyInvitationAsync Web Api 方法中,您将获得一个 DtoObject 类型的参数对象,您可以像这样在代码中使用它:
return await
_iB2CUserServiceRepository.AddUserCompanyInvitation(userid, dtoObj.UtpID , dtoObj.Email);
正确的函数签名是
[HttpGet("company/invitation/add2/{utpid}/{emailAddressInvitedUser}")]
public Task<int> AddUserCompanyInvitationAsync([FromRoute] string utpid, [FromRoute] string emailAddressInvitedUser)
如果您不添加 [FromRoute] 属性,.NET 将不会将路由值映射到您的变量。
请记住,此类请求将记录在网络服务器日志中。这意味着电子邮件地址将在日志中可见。您还需要使用 HttpUtility.UrlEncode
转义电子邮件字符串curl -X PUT "http://localhost:14786/company/invitation/add2/45/test%40test.com" -H "accept: text/plain"
首选方法是在正文中传递 Json 请求。
public class AddUserRequest
{
public int utpid { get; set; }
public string emailAddressInvitedUser { get; set; }
}
public class AddUserResponse
{
public int uid { get; set; }
}
[HttpPut("company/invitation/add1")]
public Task<AddUserResponse> AddUserCompanyInvitationAsync([FromBody]AddUserRequest addUserRequest)
{
Console.WriteLine(addUserRequest.utpid);
Console.WriteLine(addUserRequest.emailAddressInvitedUser);
return Task.FromResult(new AddUserResponse() { uid = 1 });
}
并为客户
var client = new HttpClient();
int id = 45;
string email = "test@test.com";
await client.GetAsync($"http://localhost:14786/company/invitation/add2/{id}/{System.Web.HttpUtility.UrlEncode(email)}");
var response = await client.PutAsJsonAsync("http://localhost:14786/company/invitation/add1", new AddUserRequest() { utpid = id, emailAddressInvitedUser = email });
而不是 PUT 使用 POST