asp .net core web api 中的 json 文件中具有 3 级层次结构的动态数据的 crud 操作
crud operation with dynamic data with 3 level hierarchy in json file in asp .net core web api
我想使用 asp.net 核心 Web API 在其中获取、post、放置和删除。但我不应该使用数据库来存储数据,而是我需要使用方法将动态数据存储在控制器中,也在 json 用户文件列表(userId,userName)中,3 级层次结构(国家应该有状态列表,州应该有城市列表,我可以添加国家及其州和城市)并使用它来执行 http 操作。任何人都可以帮助我一些步骤或代码吗?
我为您创建了一个示例演示,建议您不要使用 json 文件。
为什么:
使用json文件时,需要IO操作,会出现文件独占的问题
另外,在增删改查文件内容时,每次都有一个json文件被读取,然后转换成对象,然后写入文件,效率会很低
因为你要的是demo,我写的测试代码没有做任何验证。需要注意的地方可以关注一下
注册服务时必须使用AddSingleton,以保证所有用户访问同一个数据源。
当数据量很大,请求过多时,会出现数据不匹配的情况。因为我这里没有加任何锁或者限制。
测试结果
测试代码:
创建 GlobalVariablesService,并注册它
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DB_Project.Models
{
public class GlobalVariablesService
{
public List<RecordModel> records = new List<RecordModel>();
public GlobalVariablesService(){
}
public List<RecordModel> AddRecord(RecordModel r) {
records.Add(r);
return records;
}
public List<RecordModel> RemoveRecord(int rid)
{
var itemToRemove = records.Single(r => r.Record_ID == rid);
records.Remove(itemToRemove);
return records;
}
public RecordModel GetRecord(int rid)
{
var itemToGet = records.Single(r => r.Record_ID == rid);
return itemToGet;
}
}
/// <summary>
/// Add Record History
/// Rid #Uid #Country_id #Country_Name #State_id #State_Name #City_id #City_Name
/// 1 001 C1 Country1 S1 State1 C_1 City1
/// 2 002 C2 Country2 S2 State2 C_2 City2
/// 3 003 C3 Country3 S3 State3 C_3 City3
/// </summary>
///
public class RecordModel {
public int Record_ID { get; set; }
public int Uid { get; set; }
public string Country_id { set; get; }
public string Country_Name { set; get; }
public string State_id { set; get; }
public string State_Name { set; get; }
public string City_id { set; get; }
public string City_Name { set; get; }
}
public class UserModel {
public int Uid { set; get; }
public string Name { set; get; }
}
public class CountryModel
{
public string Country_id { set; get; }
public string Country_Name { set; get; }
public List<StateModel> State { set; get; }
}
public class StateModel
{
public string State_id { set; get; }
public string State_Name { set; get; }
public List<CityModel> City { set; get; }
}
public class CityModel
{
public string City_id { set; get; }
public string City_Name { set; get; }
}
}
在Startup.cs文件中注册服务;
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSingleton<GlobalVariablesService>();
}
我的测试控制器
using DB_Project.DbClass;
using DB_Project.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace DB_Project.Controllers
{
public class TestController : Controller
{
private readonly ILogger<TestController> _logger;
private readonly GlobalVariablesService _service;
public TestController(ILogger<TestController> logger, GlobalVariablesService service)
{
_logger = logger;
_service = service;
}
public IActionResult get(int rid)
{
try
{
var model = _service.GetRecord(rid);
return Ok(model);
}
catch (Exception e)
{
return Ok("error occured :" + e.ToString());
throw;
}
}
public string add(RecordModel r)
{//string uid,string countryid,string countryname,string stateid,string statename,string cityid,string cityname ) {
try
{
_service.AddRecord(r);
return "success";
}
catch (Exception)
{
return "failed";
throw;
}
}
public string delete(int rid){
try
{
_service.RemoveRecord(rid);
return "success";
}
catch (Exception)
{
return "failed";
throw;
}
}
}
}
我想使用 asp.net 核心 Web API 在其中获取、post、放置和删除。但我不应该使用数据库来存储数据,而是我需要使用方法将动态数据存储在控制器中,也在 json 用户文件列表(userId,userName)中,3 级层次结构(国家应该有状态列表,州应该有城市列表,我可以添加国家及其州和城市)并使用它来执行 http 操作。任何人都可以帮助我一些步骤或代码吗?
我为您创建了一个示例演示,建议您不要使用 json 文件。
为什么:
使用json文件时,需要IO操作,会出现文件独占的问题
另外,在增删改查文件内容时,每次都有一个json文件被读取,然后转换成对象,然后写入文件,效率会很低
因为你要的是demo,我写的测试代码没有做任何验证。需要注意的地方可以关注一下
注册服务时必须使用AddSingleton,以保证所有用户访问同一个数据源。
当数据量很大,请求过多时,会出现数据不匹配的情况。因为我这里没有加任何锁或者限制。
测试结果
测试代码:
创建 GlobalVariablesService,并注册它
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace DB_Project.Models { public class GlobalVariablesService { public List<RecordModel> records = new List<RecordModel>(); public GlobalVariablesService(){ } public List<RecordModel> AddRecord(RecordModel r) { records.Add(r); return records; } public List<RecordModel> RemoveRecord(int rid) { var itemToRemove = records.Single(r => r.Record_ID == rid); records.Remove(itemToRemove); return records; } public RecordModel GetRecord(int rid) { var itemToGet = records.Single(r => r.Record_ID == rid); return itemToGet; } } /// <summary> /// Add Record History /// Rid #Uid #Country_id #Country_Name #State_id #State_Name #City_id #City_Name /// 1 001 C1 Country1 S1 State1 C_1 City1 /// 2 002 C2 Country2 S2 State2 C_2 City2 /// 3 003 C3 Country3 S3 State3 C_3 City3 /// </summary> /// public class RecordModel { public int Record_ID { get; set; } public int Uid { get; set; } public string Country_id { set; get; } public string Country_Name { set; get; } public string State_id { set; get; } public string State_Name { set; get; } public string City_id { set; get; } public string City_Name { set; get; } } public class UserModel { public int Uid { set; get; } public string Name { set; get; } } public class CountryModel { public string Country_id { set; get; } public string Country_Name { set; get; } public List<StateModel> State { set; get; } } public class StateModel { public string State_id { set; get; } public string State_Name { set; get; } public List<CityModel> City { set; get; } } public class CityModel { public string City_id { set; get; } public string City_Name { set; get; } } }
在Startup.cs文件中注册服务;
public void ConfigureServices(IServiceCollection services) { services.AddControllersWithViews(); services.AddSingleton<GlobalVariablesService>(); }
我的测试控制器
using DB_Project.DbClass; using DB_Project.Models; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace DB_Project.Controllers { public class TestController : Controller { private readonly ILogger<TestController> _logger; private readonly GlobalVariablesService _service; public TestController(ILogger<TestController> logger, GlobalVariablesService service) { _logger = logger; _service = service; } public IActionResult get(int rid) { try { var model = _service.GetRecord(rid); return Ok(model); } catch (Exception e) { return Ok("error occured :" + e.ToString()); throw; } } public string add(RecordModel r) {//string uid,string countryid,string countryname,string stateid,string statename,string cityid,string cityname ) { try { _service.AddRecord(r); return "success"; } catch (Exception) { return "failed"; throw; } } public string delete(int rid){ try { _service.RemoveRecord(rid); return "success"; } catch (Exception) { return "failed"; throw; } } } }