我在 Visual Studio 2019 中启动了一个新的 Web 应用程序。我添加了一个 Web Api。不久之后或同时(不记得了),我开始收到这样的警告:
Warning No way to resolve conflict between "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" and "Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed". Choosing "Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed" arbitrarily.
Run Code Online (Sandbox Code Playgroud)
我注意到但没有做任何事情,因为我决定等到它给我带来问题。
而现在...
我将要使用一个 JSon 对象,但是在写这个的时候:
JObject jObject = JObject.Parse(result);
Run Code Online (Sandbox Code Playgroud)
编译器给了我这个错误:
Error CS0433 The type 'JObject' exists in both 'Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' and 'Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'
Run Code Online (Sandbox Code Playgroud)
我在网上搜索了一下,得出的结论如下:
<dependentAssembly><assemblyIdentity name="Newtonsoft.Json" …我在 Blazor 应用程序中有以下代码,它调用 API 并根据用户 ID 参数检索项目列表。
Http.GetJsonAsync 似乎不允许我输入参数。如何指定 userid 变量作为参数?
调用 API
protected override async Task OnInitializedAsync()
{
userid = await localStorage.GetItemAsync<string>("userid");
myTrades = await Http.GetJsonAsync<ItemForTrade[]>("api/Trading/GetTradesForUser");
}
Run Code Online (Sandbox Code Playgroud)
API 代码
[Authorize]
[HttpGet("[action]")]
public async Task<List<ItemForTrade>> GetTradesForUser(string userid)
{
return await tradingService.GetTradesForUser(userid);
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试调用 Web API——例如,https://myresources/getresources. 此调用需要 API 密钥、resourcesApiKey 和不记名令牌。令牌必须从另一个服务 获取https://mytokenservice/jwt。它需要一个 API 密钥 tokenServiceApiKey。
我可以通过手动访问令牌服务并输入值来获取资源数据,但我希望 Power BI 为我处理这一切。我在这里读到了一些关于此的内容,但这似乎适用于静态令牌。我们的改变了,所以我需要实际调用这项服务。
所以,我想做的是:
我认为这里描述的方法适用,但我无法让它发挥作用。以下是我最近使用 Power BI 高级编辑器尝试的操作:
我使用管理参数在 Power BI 中创建了一个参数,然后使用高级编辑器,输入了以下内容:
Web.Content(" https://mytokenservice/jwt", [ ApiKeyName="tokenServiceApiKey" ] )
我还为 tokenServiceApiKey 和 resourcesApiKey 创建了 Power BI 参数,其中包含每个参数的密钥。
不过,当我在高级编辑器中单击“完成”时,它会自动围绕我编写的内容生成一些代码,因此现在显示为:
Html.Table(Web.Content("https://mytokenservice/jwt",[ApiKeyName="tokenServiceApiKey"]), {})
Run Code Online (Sandbox Code Playgroud)
编辑器中出现的是一个不包含数据的表格图标。我应该拿回一根绳子。我不知道从这里该去哪里,并且在网上寻找答案时遇到困难。
有什么办法可以实现我所追求的目标吗?
我可能没有很好地解释它,所以如果您需要更多信息或澄清,请提问。
var list1 = _context.Employees.Where(x=> x.EmployeeId.Equals(empId)
&& **x.deptIds.Split(",")**.ToList().Any(p => (p!=(deptId.ToString()))));
Run Code Online (Sandbox Code Playgroud)
其中,x.deptIds 是以逗号分隔的字符串形式存储的部门 ID。出现错误“表达式树可能不包含使用可选参数的调用或调用”。怎么解决呢?
我的问题是,当我将有效负载正文中以零 (012345) 开头的任何整数值发送到 C# web API 时,接收到的值没有第一位数字 (12345)。它忽略了零。如何强制 API 接收原始数据?
[HttpPost]
public void insertdata([FromBody]Model model)
{
// model.id=1234
}
Run Code Online (Sandbox Code Playgroud)
有效载荷
{id:01234}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用abp.io编写一个 Web api并实现NodaTime。每当我尝试调用 POST 操作时,我都会得到
验证期间检测到以下错误。\r\n - JSON 值无法转换为 NodaTime.LocalDate。
我已经在模块中正确配置了 NodaTime,我还在传统的 .net 5 Web api 中对其进行了测试,并且工作正常。
这是我的配置:
private void ConfigureNodaSerialization()
{
Configure<JsonSerializer>(options =>
{
options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
//options.Converters.Add(NodaConverters.LocalDateConverter);
});
}
Run Code Online (Sandbox Code Playgroud)
我正在使用 NodaTime.Serialization.JsonNet
我也尝试过 NodaTime.Serialization.SystemTextJson 无济于事,结果相同。
..这是应用程序服务:
[AllowAnonymous]
public class NodaTestAppService : TestAppService, INodaTestAppService
{
public Task PostNodaTest([FromBody]NodaTestDto dto)
{
return Task.CompletedTask;
}
public Task GetNodaTest(NodaTestDto dto)
{
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
GET 工作正常,问题出在 POST 上。
这是 dto:
public class NodaTestDto
{
public LocalDate Date { get; set; } …Run Code Online (Sandbox Code Playgroud) 我在 .net 6.0 上有 c# webapi。我正在将 postdata 从客户端获取到 param_class 到 _input 中。该类如下:
public class param_class
{
public string? name { get; set; }
public object? value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
客户端以 json 字符串形式发送数据,如下所示:
{
"name": "lotnum",
"value": 143
}
Run Code Online (Sandbox Code Playgroud)
在 webapi 中,当我获取值字段时,会抛出错误 JsonElement 无法转换为 Int。
var v = (int)_input.value;
Run Code Online (Sandbox Code Playgroud)
如何在.net 6.0的webapi中将JsonElement转换为其他类型?
我有类似的问题。\n是否有更简单的方法来处理输入数据类型错误?
\n\nhttps://coderethinked.com/customizing-automatic-http-400-error-response-in-asp-net-core-web-apis/
\n\n我搜索但没有找到答案。
\n\n更新:
\n\n我从微软的例子中找到了一个解决方案:\n\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0\xc2\xa0
\n\n return BadRequest (new {message = "Something went wrong"});\nRun Code Online (Sandbox Code Playgroud)\n\n但是\n当字段为[必填]时模型时,在运行服务之前会调用错误 400。有没有办法解决句柄模型的 400 错误?
\n我在 .NET Core 项目中使用 OpenAPI (Swagger),当使用多个具有类似获取请求的方法时,我遇到“Microsoft.AspNetCore.Routing.Matching.AmbiguousMatchException:请求匹配多个端点”。运行时出错。我查看了 web 和 SO 上的几个页面,并尝试应用诸如请求匹配多个端点之类的解决方法,但为什么呢?,但这并不能解决问题。以下是我使用的 API 方法和路由定义。
[Route("get", Name="get")]
public IEnumerable<DemoDto> Get()
{
//
}
[Route("get/{id}", Name="getById")]
public DemoDto GetById(int id)
{
//
}
[Route("get/{query}", Name="getWithPagination")]
public IEnumerable<DemoDto> GetWithPagination(DemoQuery query)
{
//
}
Run Code Online (Sandbox Code Playgroud)
我使用Name财产来解决问题但没有解决。有什么想法可以改变路线以区分Get()和GetWithPagination()吗?
为什么我们在 WebApi c# 中需要异步任务,无论如何,默认的 Web API 请求将仅通过创建或重用现有线程来运行。那么线程已经被使用了?