.net 如何在Blazor中使用ProblemDetails类

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

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

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

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

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

以下是库的csproj文件:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Include="Microsoft.AspNetCore.App" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\DataLayer\DataLayer.csproj" />
    <ProjectReference Include="..\BizLogic\BizLogic.csproj" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper" Version="12.0.0" />
    <PackageReference Include="Dapper" Version="2.0.123" />
    <PackageReference Include="FluentValidation" Version="10.4.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.10" />
    <PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

</Project>

这里是blazor wasm.csproj文件:

<Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="12.0.1" />
    <PackageReference Include="Blazored.FluentValidation" Version="2.0.1" />
    <PackageReference Include="FluentValidation" Version="11.7.1" />
    <PackageReference Include="FluentValidation.DependencyInjectionExtensions" Version="11.7.1" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.8" />
    <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer"
      Version="6.0.8" PrivateAssets="all" />
    <PackageReference Include="MudBlazor" Version="6.10.0" />
    <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\ServiceLayer\ServiceLayer.csproj" />
    <ProjectReference Include="..\BizLogic\BizLogic.csproj" />
  </ItemGroup>

</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。
但现在我得到了一个不同的错误:

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


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

public async Task AddServiceResponsesAsync(List<ServiceResponse> responses)
        {
            HttpClient client = _clientFactory.CreateClient();

            string uri = _configuration.GetConnectionString("web-api-uri-base-adress");

            client.BaseAddress = new Uri(uri);

            JsonSerializer serializer = JsonSerializer.Create();
            StringWriter stringWriter = new StringWriter();
            serializer.Serialize(stringWriter, responses);

            StringContent content = new StringContent(stringWriter.ToString(), Encoding.UTF8, "application/json");

            var response = await client.PostAsync("service-responses", content);

                    var detailedError = await response.Content.ReadFromJsonAsync<ProblemDetails>();
}


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

nimxete2

nimxete21#

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

public class ClientProblemDetails
{
    [JsonPropertyName("type")]
    public string Type { get; set; } = default!;

    [JsonPropertyName("title")]
    public string Title { get; set; } = default!;

    [JsonPropertyName("status")]
    public int Status { get; set; } = default!;

    [JsonPropertyName("detail")]
    public string Detail { get; set; } = default!;

    [JsonPropertyName("instance")]
    public string Instance { get; set; } = default!;
}

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

if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
{
    ClientProblemDetails clientProblemDetails = httpResponse.Content
        .ReadFromJsonAsync<ClientProblemDetails>();

    // Pass clientProblemDetails around the client as you wish
    // or throw a custom "ApiCallException", so can catch
    // exceptions from higher up the stack and inspect with
    // ex.ProblemDetails.Type, etc.
    // e.g.:
    throw new ApiCallException(clientProblemDetails);
}

class ApiCallException : Exception
{
    public ClientProblemDetails ProblemDetails { get; }

    public ApiCallException(ClientProblemDetails details) : base(details.Title)
    {
        ProblemDetails = details;
    }

}

相关问题