如何在 Blazor WebAssembly 中将 Json 结果转换为字符串? [英] How to covert Json result into string in Blazor WebAssembly?

查看:53
本文介绍了如何在 Blazor WebAssembly 中将 Json 结果转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把结果转成字符串传递给导航路径,但是我做不到,请帮帮我.

I want to convert the result into a string and pass it to the navigation path, but I couldn't do it, please help me.

HttpGet 控制器

HttpGet Controller

[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);
}

控制器返回结果

85e39a3e-8101-4166-9193-5e41bec1a7ce"

"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");
            }

        }
    }

推荐答案

好的,我可以看到一些问题.

OK, I can see a few problems.

在服务器上:

[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);
}

首先,这里的返回类型是 Task> .ApplicationUser 与后端身份库相关联,您不能也不应该将其用于 DTO.

First, your return type here is Task<ActionResult<ApplicationUser>> . ApplicationUser is tied to the backend Identity library, you can't and shouldn't use it for a DTO.

你没有,最后你有 return new JsonResult(result); 当你将返回类型更改为 Task.

And you don't, in the end you have return new JsonResult(result); which is OK when you change the return type to just Task<ActionResult>.

在客户端:

//user = await Http.GetFromJsonAsync<userName>("Authentication/UserId?Username=" + Username);
  var userId = await Http.GetFromJsonAsync<string>("Authentication/UserId?Username=" + Username);

端点返回一个简单的字符串.Json 不知道用户名"或其他任何内容.

The endpoint returns a simple string. Json does not know about 'UserName' or anything else.

//string Id = System.Text.Json.JsonSerializer.Serialize(user); -- use UserId

您在此处(再次)序列化了 Id,几乎可以肯定它对于 URL 无效.所以就跳过那个.

You are serializing the Id (again) here, making it almost certainly invalid for an URL. So just skip that.

这篇关于如何在 Blazor WebAssembly 中将 Json 结果转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆