![](/img/trans.png)
[英]implement CRUD functionality with static data using JSON in asp.net core api project
[英]crud operation with dynamic data with 3 level hierarchy in json file in asp .net core web api
我想使用 asp.net 核心 Web API 在其中進行獲取、發布、放置和刪除操作。但是我不應該使用數據庫來存儲數據,而是我需要使用方法將動態數據存儲在 controller 以及 json 用戶文件列表中( userId,userName),3 級層次結構(國家應該有 state 列表,State 應該有城市列表,我可以添加國家及其州和城市)並使用它來執行 http 操作。 任何人都可以幫我一些步驟或代碼嗎?
我為您創建了一個示例演示,我建議您不要使用 json 文件。
為什么:
當你使用json個文件時,需要IO次操作,會出現文件獨占的問題。
另外,在增刪改查文件內容的時候,每次都有一個json的文件被讀取,然后轉換成一個object,再寫入文件,效率會很低。
因為你要的是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>(); }
我的測試 Controller
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; } } } }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.