如何在 Blazor WebAssembly 中将 Json 结果转换为字符串?
How to covert Json result into string in Blazor WebAssembly?
我想把结果转成字符串传给导航路径,但是做不到,请帮帮我
HttpGet 控制器
[HttpGet]
[Route("UserId")]
public async Task<ActionResult<ApplicationUser>> GetUserId(string Username)
{
var user = await userManager.FindByNameAsync(Username);
if (user == null)
return StatusCode(StatusCodes.Status500InternalServerError, new Response { Status = "Error", Message = "User not exist" });
var result = await userManager.GetUserIdAsync(user);
return new JsonResult(result);
}
控制器return 结果
"85e39a3e-8101-4166-9193-5e41bec1a7ce"
函数
private async Task Login()
{
var user = new userName { Username = Username };
var loginUser = new LoginDb { Username = Username, Password = Password };
if (Username == null || Password == null)
{
toastService.ShowWarning("Please enter Username and Password");
}
else
{
user = await Http.GetFromJsonAsync<userName>("Authentication/UserId?Username=" + Username);
if (user != null)
{
string Id = System.Text.Json.JsonSerializer.Serialize(user);
var result = await Http.PostAsJsonAsync("Authentication/login", loginUser);
if (result.IsSuccessStatusCode)
{
NavigationManager.NavigateTo("/profile/" + Id);
toastService.ShowSuccess("Login successful");
}
else
{
toastService.ShowError("Username or Password is wrong");
}
}
else
{
NavigationManager.NavigateTo("/login");
}
}
}
好的,我可以看到一些问题。
在服务器上:
[HttpGet]
[Route("UserId")]
public async Task<ActionResult<ApplicationUser>> GetUserId(string Username) // A
{
var user = await userManager.FindByNameAsync(Username);
if (user == null) // B
return StatusCode(StatusCodes.Status500InternalServerError, new Response { Status = "Error", Message = "User not exist" });
var result = await userManager.GetUserIdAsync(user);
return new JsonResult(result);
}
首先,您的 return 类型是 Task<ActionResult<ApplicationUser>>
。 ApplicationUser 绑定到后端身份库,你不能也不应该将它用于 DTO。
而你没有,最后你有 return new JsonResult(result);
当你将 return 类型更改为 Task<ActionResult>
.
时就可以了
在客户端:
//user = await Http.GetFromJsonAsync<userName>("Authentication/UserId?Username=" + Username);
var userId = await Http.GetFromJsonAsync<string>("Authentication/UserId?Username=" + Username);
端点 return 是一个简单的字符串。 Json 不知道 'UserName' 或其他任何事情。
//string Id = System.Text.Json.JsonSerializer.Serialize(user); -- use UserId
您在这里(再次)序列化了 Id,几乎可以肯定它对 URL 无效。所以跳过那个。
我想把结果转成字符串传给导航路径,但是做不到,请帮帮我
HttpGet 控制器
[HttpGet]
[Route("UserId")]
public async Task<ActionResult<ApplicationUser>> GetUserId(string Username)
{
var user = await userManager.FindByNameAsync(Username);
if (user == null)
return StatusCode(StatusCodes.Status500InternalServerError, new Response { Status = "Error", Message = "User not exist" });
var result = await userManager.GetUserIdAsync(user);
return new JsonResult(result);
}
控制器return 结果
"85e39a3e-8101-4166-9193-5e41bec1a7ce"
函数
private async Task Login()
{
var user = new userName { Username = Username };
var loginUser = new LoginDb { Username = Username, Password = Password };
if (Username == null || Password == null)
{
toastService.ShowWarning("Please enter Username and Password");
}
else
{
user = await Http.GetFromJsonAsync<userName>("Authentication/UserId?Username=" + Username);
if (user != null)
{
string Id = System.Text.Json.JsonSerializer.Serialize(user);
var result = await Http.PostAsJsonAsync("Authentication/login", loginUser);
if (result.IsSuccessStatusCode)
{
NavigationManager.NavigateTo("/profile/" + Id);
toastService.ShowSuccess("Login successful");
}
else
{
toastService.ShowError("Username or Password is wrong");
}
}
else
{
NavigationManager.NavigateTo("/login");
}
}
}
好的,我可以看到一些问题。
在服务器上:
[HttpGet]
[Route("UserId")]
public async Task<ActionResult<ApplicationUser>> GetUserId(string Username) // A
{
var user = await userManager.FindByNameAsync(Username);
if (user == null) // B
return StatusCode(StatusCodes.Status500InternalServerError, new Response { Status = "Error", Message = "User not exist" });
var result = await userManager.GetUserIdAsync(user);
return new JsonResult(result);
}
首先,您的 return 类型是 Task<ActionResult<ApplicationUser>>
。 ApplicationUser 绑定到后端身份库,你不能也不应该将它用于 DTO。
而你没有,最后你有 return new JsonResult(result);
当你将 return 类型更改为 Task<ActionResult>
.
在客户端:
//user = await Http.GetFromJsonAsync<userName>("Authentication/UserId?Username=" + Username);
var userId = await Http.GetFromJsonAsync<string>("Authentication/UserId?Username=" + Username);
端点 return 是一个简单的字符串。 Json 不知道 'UserName' 或其他任何事情。
//string Id = System.Text.Json.JsonSerializer.Serialize(user); -- use UserId
您在这里(再次)序列化了 Id,几乎可以肯定它对 URL 无效。所以跳过那个。