.net 如何在Blazor中使用ProblemDetails类

hgtggwj0  于 2023-11-20  发布在  .NET
关注(0)|答案(1)|浏览(233)

我有一个关于在blazor应用程序中使用框架引用的问题。
我尝试使用ProblemsDetails类,它是Microsoft.AspNetCore.Mvc共享框架的一部分。Blazor应用参考使用的库之一

标签在.csproj文件中。
当我尝试构建Blazor应用程序时,我得到:

  1. error NETSDK1082: There was no runtime pack for M
  2. icrosoft.AspNetCore.App available for the specified RuntimeIdentifier 'browser-wasm'.

字符串
解决办法是什么?
有没有办法在blazor中引用ProblemDetails类和相关的类,比如ValidationProblemDetails?

以下是库的csproj文件:

  1. <Project Sdk="Microsoft.NET.Sdk">
  2. <PropertyGroup>
  3. <TargetFramework>net6.0</TargetFramework>
  4. <ImplicitUsings>enable</ImplicitUsings>
  5. <Nullable>enable</Nullable>
  6. </PropertyGroup>
  7. <ItemGroup>
  8. <FrameworkReference Include="Microsoft.AspNetCore.App" />
  9. </ItemGroup>
  10. <ItemGroup>
  11. <ProjectReference Include="..\DataLayer\DataLayer.csproj" />
  12. <ProjectReference Include="..\BizLogic\BizLogic.csproj" />
  13. </ItemGroup>
  14. <ItemGroup>
  15. <PackageReference Include="AutoMapper" Version="12.0.0" />
  16. <PackageReference Include="Dapper" Version="2.0.123" />
  17. <PackageReference Include="FluentValidation" Version="10.4.0" />
  18. <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
  19. <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
  20. <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  21. </ItemGroup>
  22. </Project>

这里是blazor wasm.csproj文件:

  1. <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">
  2. <PropertyGroup>
  3. <TargetFramework>net6.0</TargetFramework>
  4. <Nullable>enable</Nullable>
  5. <ImplicitUsings>enable</ImplicitUsings>
  6. </PropertyGroup>
  7. <ItemGroup>
  8. <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
  9. <PackageReference Include="Blazored.FluentValidation" Version="2.0.1" />
  10. <PackageReference Include="FluentValidation" Version="11.7.1" />
  11. <PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
  12. <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.8" />
  13. <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
  14. Version="6.0.8" PrivateAssets="all" />
  15. <PackageReference Include="MudBlazor" Version="6.10.0" />
  16. <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  17. </ItemGroup>
  18. <ItemGroup>
  19. <ProjectReference Include="..\ServiceLayer\ServiceLayer.csproj" />
  20. <ProjectReference Include="..\BizLogic\BizLogic.csproj" />
  21. </ItemGroup>
  22. </Project>


问题:https://github.com/dotnet/aspnetcore/issues/36970
此分类上一篇:https://github.com/dotnet/aspnetcore/issues/30910
从前面的链接我试图取代框架参考Microsoft.AspNetCore.Mvc使用Microsoft.AspNetCore.Mvc.Core包。
似乎引用Microsoft.AspNetCore.Mvc.Core可以解决前面提到的错误NETSDK1082。
但现在我得到了一个不同的错误:

  1. error CS0234: The type or namespace name 'ApplicationPartAttributeA
  2. ttribute' does not exist in the namespace 'Microsoft.AspNetCore.Mvc.ApplicationParts' (are you missing an assembly reference?)


下面是使用ProblemDetails的库中的代码摘录:

  1. public async Task AddServiceResponsesAsync(List<ServiceResponse> responses)
  2. {
  3. HttpClient client = _clientFactory.CreateClient();
  4. string uri = _configuration.GetConnectionString("web-api-uri-base-adress");
  5. client.BaseAddress = new Uri(uri);
  6. JsonSerializer serializer = JsonSerializer.Create();
  7. StringWriter stringWriter = new StringWriter();
  8. serializer.Serialize(stringWriter, responses);
  9. StringContent content = new StringContent(stringWriter.ToString(), Encoding.UTF8, "application/json");
  10. var response = await client.PostAsync("service-responses", content);
  11. var detailedError = await response.Content.ReadFromJsonAsync<ProblemDetails>();
  12. }


我好像做错了什么。请帮帮忙。谢谢。

nimxete2

nimxete21#

从你的问题中,我推测你想在Blazor Wasm Client项目中解析从服务器返回的ProblemDetails,这样你就可以在客户端上检查ProblemDetails并相应地执行操作。
要记住的关键是,从服务器返回的数据只是从ProblemDetails类序列化的Json。
因此,任何具有相同属性的类都适合于示例化。
我的解决方案是创建一个ClientProblemDetails类:

  1. public class ClientProblemDetails
  2. {
  3. [JsonPropertyName("type")]
  4. public string Type { get; set; } = default!;
  5. [JsonPropertyName("title")]
  6. public string Title { get; set; } = default!;
  7. [JsonPropertyName("status")]
  8. public int Status { get; set; } = default!;
  9. [JsonPropertyName("detail")]
  10. public string Detail { get; set; } = default!;
  11. [JsonPropertyName("instance")]
  12. public string Instance { get; set; } = default!;
  13. }

字符串
然后,如果来自服务器的响应是BadRequest,则将其转换为ClientProblemDetails,然后将其传递给客户端应用程序。这样,您就不需要从客户端引用Microsoft.AspNetCore.App框架。

  1. if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
  2. {
  3. ClientProblemDetails clientProblemDetails = httpResponse.Content
  4. .ReadFromJsonAsync<ClientProblemDetails>();
  5. // Pass clientProblemDetails around the client as you wish
  6. // or throw a custom "ApiCallException", so can catch
  7. // exceptions from higher up the stack and inspect with
  8. // ex.ProblemDetails.Type, etc.
  9. // e.g.:
  10. throw new ApiCallException(clientProblemDetails);
  11. }
  12. class ApiCallException : Exception
  13. {
  14. public ClientProblemDetails ProblemDetails { get; }
  15. public ApiCallException(ClientProblemDetails details) : base(details.Title)
  16. {
  17. ProblemDetails = details;
  18. }
  19. }

展开查看全部

相关问题