如何从 ASP.NET Core WebAPI 获取 int 值?
Posted
技术标签:
【中文标题】如何从 ASP.NET Core WebAPI 获取 int 值?【英文标题】:How to get int value from ASP.NET Core WebAPI? 【发布时间】:2021-04-09 11:47:01 【问题描述】:我正在使用 ASP.NET Core Blazor WebAssembly、.NET 5.0.1、Entity Framework Core 5.0.1、Microsoft SQL Server、DevExpress Blazor WebAssembly 20.2.4。
在服务器项目(带有 Identity Server 4 的 ASP.NET Core Web API)
using acc.Server.Models;
using acc.Server.Data;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace acc.Server.Controllers
[Authorize]
[ApiController]
[Route("[controller]")]
public class OrganizationUnitController : ControllerBase
private readonly ApplicationDbContext db;
private readonly ILogger<OrganizationUnitController> _logger;
public OrganizationUnitController(ILogger<OrganizationUnitController> logger, ApplicationDbContext _db)
_logger = logger;
db = _db;
// https://localhost:5001/OrganizationUnit
[HttpGet]
public IEnumerable<OrganizationUnit> Get()
try
return db.OrganizationUnits.ToList();
catch
throw;
// POST: https://localhost:5001/OrganizationUnit
[HttpPost]
public async Task<ActionResult<OrganizationUnit>> PostItem(OrganizationUnit item)
db.OrganizationUnits.Add(item);
await db.SaveChangesAsync();
return CreatedAtAction(nameof(GetItem), new id = item.Id , item);
// GET: https://localhost:5001/OrganizationUnit/5
[HttpGet("id")]
public async Task<ActionResult<OrganizationUnit>> GetItem(int id)
var item = await db.OrganizationUnits.FindAsync(id);
if (item == null)
return NotFound();
return item;
// https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
// https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-5.0&tabs=visual-studio
// DELETE
[HttpDelete("id")]
public async Task<IActionResult> DeleteItem(int id)
var item = await db.OrganizationUnits.FindAsync(id);
if (item == null)
return NotFound();
db.OrganizationUnits.Remove(item);
await db.SaveChangesAsync();
return NoContent();
[HttpGet("GetGradeOfOrganizationUnit/orgUnitId")]
public async Task<int> GetGradeOfOrganizationUnit(int orgUnitId)
var item = await db.OrganizationUnits.FindAsync(orgUnitId);
int orgUnitGrade = 1;
if (item.Grade != null)
orgUnitGrade = (int)item.Grade;
return orgUnitGrade;
在客户端项目(Blazor WebAssembly)
@page "/OrganizationUnitPage"
@inject HttpClient Http
<h3>Cơ cấu tổ chức</h3>
<DxDataGrid Data="@OrganizationUnits"
ShowPager="true"
RowRemoving="@((dataItem) => OnRowRemoving(dataItem))"
RowInserting="@((newValues) => OnRowInserting(newValues))"
RowUpdating="@((updatingDataItem, newValues) => OnRowUpdating(updatingDataItem, newValues))"
PagerPageSizeSelectorVisible="true"
PagerAllowedPageSizes=@(new int[] 10,30,50)
HorizontalScrollBarMode="ScrollBarMode.Visible"
T="OrganizationUnit"
ColumnResizeMode="@DataGridColumnResizeMode.Component"
ShowFilterRow="true"
CssClass="mw-1100"
EditMode="DataGridEditMode.PopupEditForm">
<HeaderTemplate>
<DxToolbar>
<DxDataGridColumnChooserToolbarItem Alignment="ToolbarItemAlignment.Right" Text="Chọn cột" Tooltip="Chọn cột" />
</DxToolbar>
</HeaderTemplate>
<Columns>
<DxDataGridCommandColumn Width="150px"></DxDataGridCommandColumn>
<DxDataGridColumn Field="@nameof(OrganizationUnit.OrganizationUnitCode)" Caption="Mã đơn vị"></DxDataGridColumn>
<DxDataGridColumn Field="@nameof(OrganizationUnit.OrganizationUnitName)" Caption="Tên đơn vị"></DxDataGridColumn>
<DxDataGridColumn Field="@nameof(OrganizationUnit.Address)" Caption="Địa chỉ"></DxDataGridColumn>
<DxDataGridComboBoxColumn T="ComboBoxItem"
Field="@nameof(OrganizationUnit.Grade)"
Caption="Cấp tổ chức"
Data="@ListOfAccountTypes"
ValueFieldName="@nameof(ComboBoxItem.ComboBoxItemValue)"
TextFieldName="@nameof(ComboBoxItem.ComboBoxItemLabel)"
Width="200px">
<DisplayTemplate>
@
var foo = (context as OrganizationUnit).Grade;
int value = 0;
if (foo != null)
value = foo.Value;
string str = "";
switch (value)
case 1:
str = "Tổng công ty/Công ty";
break;
case 2:
str = "Phòng ban";
break;
default:
str = "";
break;
<div>@str</div>
</DisplayTemplate>
</DxDataGridComboBoxColumn>
<DxDataGridCheckBoxColumn Field="@nameof(OrganizationUnit.ActiveStatus)"
Caption="Theo dõi"
FilterTextChecked="Theo dõi"
FilterTextUnchecked="Ngừng theo dõi"
FilterTextIndeterminate="Không xác định"
ValueChecked="@ActiveStatus.Active"
ValueUnchecked="@ActiveStatus.NotActive"></DxDataGridCheckBoxColumn>
<DxDataGridColumn Field="@nameof(OrganizationUnit.Id)" Caption="Id" Visible="false"></DxDataGridColumn>
</Columns>
<TotalSummary>
<DxDataGridSummaryItem ShowInColumn=@nameof(OrganizationUnit.OrganizationUnitName) SummaryType=SummaryItemType.Count DisplayFormat="Số dòng: 0" />
</TotalSummary>
</DxDataGrid>
@code
IEnumerable<OrganizationUnit> OrganizationUnits;
IEnumerable<ComboBoxItem> ListOfAccountTypes;
public enum ActiveStatus
NotActive,
Active
// Khởi tạo
protected override async Task OnInitializedAsync()
OrganizationUnits = await Http.GetFromJsonAsync<OrganizationUnit[]>("OrganizationUnit");
ListOfAccountTypes = new List<ComboBoxItem>()
new ComboBoxItem(0,"Dư Nợ"),
new ComboBoxItem(1,"Dư Có"),
new ComboBoxItem(2,"Lưỡng tính")
;
public void OnRegionChanged()
InvokeAsync(StateHasChanged);
// Xóa
void OnRowRemoving(OrganizationUnit organizationUnit)
Http.DeleteAsync("OrganizationUnit/" + organizationUnit.Id);
InvokeAsync(StateHasChanged);
// Cập nhật
void OnRowUpdating(OrganizationUnit organizationUnit, Dictionary<string, object> newValue)
foreach (var field in newValue.Keys)
switch (field)
case "OrganizationUnitCode": // Mã đơn vị
organizationUnit.OrganizationUnitCode = (string)newValue[field];
break;
case "OrganizationUnitName": // Tên đơn vị
organizationUnit.OrganizationUnitName = (string)newValue[field];
break;
case "ParentId": // Thuộc đơn vị
organizationUnit.ParentId = (int)newValue[field];
break;
case "Grade": // Cấp tổ chức
organizationUnit.Grade = (int)newValue[field];
break;
case "CostAccount": // Tài khoản chi phí lương
organizationUnit.CostAccount = (string)newValue[field];
break;
organizationUnit.BranchId = 1;
organizationUnit.IsSystem = false;
if(organizationUnit.ParentId != null)
if(organizationUnit.ParentId == null)
// Lấy grade của parent
string endPoint = "OrganizationUnit/GetGradeOfOrganizationUnit/" + organizationUnit.ParentId;
int orgUnitParentGrade = await Http.GetFromJsonAsync<int>(endPoint);
//int parentGrade =
// Sau đó lấy grade của parent cộng thêm 1 để ra grade của Đơn vị hiện tại.
organizationUnit.Grade = 3;
// Lưu vào database.
Http.PutAsJsonAsync("OrganizationUnit", organizationUnit);
InvokeAsync(StateHasChanged);
// Thêm mới
void OnRowInserting(Dictionary<string, object> newValue)
var organizationUnit = new OrganizationUnit();
foreach (var field in newValue.Keys)
switch (field)
case "OrganizationUnitCode": // Mã đơn vị
organizationUnit.OrganizationUnitCode = (string)newValue[field];
break;
case "OrganizationUnitName": // Tên đơn vị
organizationUnit.OrganizationUnitName = (string)newValue[field];
break;
case "ParentId": // Thuộc đơn vị
organizationUnit.ParentId = (int)newValue[field];
break;
case "Grade": // Cấp tổ chức
organizationUnit.Grade = (int)newValue[field];
break;
case "CostAccount": // Tài khoản chi phí lương
organizationUnit.CostAccount = (string)newValue[field];
break;
organizationUnit.BranchId = 1;
organizationUnit.IsSystem = false;
if (organizationUnit.ParentId != null)
if (organizationUnit.ParentId == null)
// Lấy grade của parent
// Sau đó lấy grade của parent cộng thêm 1 để ra grade của Đơn vị hiện tại.
organizationUnit.Grade = 3;
// Lưu vào database.
Http.PostAsJsonAsync("OrganizationUnit", organizationUnit);
// Tạo ra sự thay đổi ở phía client mà không lấy lại dữ liệu từ phía database.
// OrganizationUnits = (new OrganizationUnit[] newItem ).Concat(OrganizationUnits);
// Gọi lại dữ liệu từ database.
InvokeAsync(StateHasChanged);
[Parameter] public string Action get; set;
ElementReference input;
关注这几行 (1)
string endPoint = "OrganizationUnit/GetGradeOfOrganizationUnit/" + organizationUnit.ParentId;
int orgUnitParentGrade = await Http.GetFromJsonAsync<int>(endPoint);
但是(1)处的源代码是语法错误。如何解决?
【问题讨论】:
我想指出您的问题的一些问题(我认为这将解释否决票)。 1)对于其他人来说,使用一小段代码会更好。您的问题很长,似乎没有必要。例如,后端代码是多余的。 2)文本而不是文本/代码的屏幕截图被普遍接受,因为它更加用户友好。希望这会有所帮助! 【参考方案1】:你能改变吗:
int orgUnitParentGrade = await Http.GetFromJsonAsync<int>(endPoint);
收件人:
var response = await Http.GetAsync(endPoint);
if (response.IsSuccessStatusCode)
var data = await response.Content.ReadAsStringAsync();
var orgUnitParentGrade = JsonConvert.DeserializeObject<int>(data);
else....
【讨论】:
【参考方案2】:我认为真正的问题是方法void OnRowUpdating(...)
没有标记为异步。
这就是您提出的解决方案有效的原因 - 通过强制异步结果看起来像它是同步的(这就是 .Result
所做的)。但一般来说,这不是你应该“解决”这些问题的方式——这样你就可以混合异步和同步代码。这是一个非常受欢迎的anti-pattern(第 4 部分)。
更好的解决方案是让您的方法 OnRowUpdating
异步,同时将其中的所有内容保持原样:
async Task OnRowUpdating(...)
...
int orgUnitParentGrade = await Http.GetFromJsonAsync<int>(endPoint);
...
更新。再次查看您的代码后,我认为您应该仔细检查OnRegionChanged
,OnRowInserting
,OnRowUpdating
,OnRowRemoving
,并为每一个寻找两件事:
它们本质上是异步的,但它们每个都有同步签名。所以你应该像我为OnRowUpdating
做的那样让它们异步。
它们包含诸如Http.DeleteAsync
、InvokeAsync
等异步方法的调用,而无需等待它们的结果。你确定这是故意的吗?如果没有,等待应该等待的!
您还可以查看async programming model in .net 的文档以加深对它的理解。
【讨论】:
以上是关于如何从 ASP.NET Core WebAPI 获取 int 值?的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET Core - 从 WebApi 提供静态内容 [重复]
如何从 asp.net core webapi 获取数据到连接到数据库的 angular 11.0.0。我试图这样做,但它返回空记录
选择 webApi 模板时如何将 ASP.Net 身份添加到 Asp.Net Core?