如何在c#中将变量传递给前端
How to pass variables in c# to frontend
我正在使用反向地理编码来获取可读的用户地址,即使用 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>
我想删除这些行,基本上只有一行显示可读地址。
将属性地址添加到代码后面
public string Address {get; set;}
....
public async Task<IActionResult> OnGetAsync(int? id)
{
.....
Address= "Country - " + country +" Region "+region;
}
和页面
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Address)
</dd>
我正在使用反向地理编码来获取可读的用户地址,即使用 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>
我想删除这些行,基本上只有一行显示可读地址。
将属性地址添加到代码后面
public string Address {get; set;}
....
public async Task<IActionResult> OnGetAsync(int? id)
{
.....
Address= "Country - " + country +" Region "+region;
}
和页面
<dt class="col-sm-2">
@Html.DisplayNameFor(model => model.Address)
</dt>
<dd class="col-sm-10">
@Html.DisplayFor(model => model.Address)
</dd>