问题描述
我正在使用反向地理编码来获取可读的用户地址,即使用 restful api 的国家和地区。我想在前端显示它,但我不确定如何将它传递给前端。我可以根据自己的喜好解析 json 响应并获取我需要的数据,我只想将这两个变量显示为一个串联字符串。我在用户模型中有经度和纬度字段。也许变量超出了范围?
后端
namespace VescovererWebApp.Pages
{
public class AccountModel : PageModel
{
private readonly VescovererWebApp.Data.ApplicationDbContext _context;
public AccountModel(VescovererWebApp.Data.ApplicationDbContext context)
{
_context = context;
}
public User User { get; set; }
public async Task<IActionResult> OnGetAsync(int? id)
{
if (id == null)
{
return NotFound();
}
User = await _context.User.FirstOrDefaultAsync(m => m.ID == id);
using (var httpClient = new HttpClient())
{
using (var response = await httpClient.GetAsync($"http://api.positionstack.com/v1/reverse?access_key={apikey}&query={User.Latitude},{User.Longitude}"))
{
string apiResponse = await response.Content.ReadAsStringAsync();
JObject o = JObject.Parse(apiResponse);
var region = o["data"][0]["region"];
var country = o["data"][0]["country"];
System.Diagnostics.Debug.WriteLine(region);
System.Diagnostics.Debug.WriteLine(country);
}
}
if (User == null)
{
return NotFound();
}
return Page();
}
}
}
**Razor page**
<div>
<hr />
<dl class="row">
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Longitude)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Longitude)
</dd>
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.User.Latitude)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.User.Latitude)
</dd>
</dl>
</div>
我想删除这些行,基本上只有一个显示可读地址的行。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)