[英]Blazor requesting a json file
I am trying to retrieve data from the server.我正在尝试从服务器检索数据。 But I can't get the json file from the server.但我无法从服务器获取 json 文件。 I placed my json file in wwwroot/data/Users.json.我将我的 json 文件放在 wwwroot/data/Users.json 中。 This is my code.这是我的代码。 I receive this exception: Unhandled exception rendering component: Could not find a part of the path "/data/Users.json".我收到此异常:未处理的异常呈现组件:找不到路径“/data/Users.json”的一部分。
@page "/"
@code{
public class User
{
public string username;
public string password;
}
}
@{
string json = System.IO.File.ReadAllText("data/Users.json");
User mahan = Newtonsoft.Json.JsonConvert.DeserializeObject<User>(json);
<body>
<h1>Usernsme: @mahan.username</h1>
</body>
In Blazor Server you can use ReadAllText("wwwroot/data/Users.json");
在 Blazor 服务器中,您可以使用ReadAllText("wwwroot/data/Users.json");
In Blazor WebAssembly you don't provide the wwwroot/
part but since ReadAllText() is not supported there it doesn't really matter.在 Blazor WebAssembly 中,您不提供wwwroot/
部分,但由于 ReadAllText() 在那里不受支持,这并不重要。
Under WebAssembly you can await Http.GetStringAsync("data/Users.json");
在 WebAssembly 下,您可以await Http.GetStringAsync("data/Users.json");
Check FetchData.razor and weather.json for a full example.查看 FetchData.razor 和 weather.json 以获得完整示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.