如何在 Blazor 应用程序中从 Http.PutAsJsonAsync 传递多个字符串值

时间:2021-07-24 03:09:28

标签: c# html blazor blazor-webassembly

我需要发布两个字符串值才能在数据库中添加公司。这里我把 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);
        }

3 个答案:

答案 0 :(得分:0)

替换

await Http.PutAsJsonAsync($"api/B2CUsers/company/invitation/add/{UtpID}/{User.email}",
                 UtpID,User.email);

await Http.PutAsync($"api/B2CUsers/company/invitation/add/{UtpID}/{User.email}",
                 null);

PutAsJsonAsync 需要 JSon 负载,但您已将所有信息放入 URL。 Su 使用更直接的 PutAsync 并为内容传递 null

答案 1 :(得分:0)

注意:习惯上将单个值传递给 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);

答案 2 :(得分:0)

正确的函数签名是

[HttpGet("company/invitation/add2/{utpid}/{emailAddressInvitedUser}")]
public Task<int> AddUserCompanyInvitationAsync([FromRoute] string utpid, [FromRoute] string emailAddressInvitedUser)

如果您不添加 [FromRoute] 属性,.NET 将不会将路由值映射到您的变量。

请记住,此类请求将记录在 Web 服务器日志中。这意味着电子邮件地址将在日志中可见。您还需要使用 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 });

使用POST代替PUT