1.新建netcore空项目
2.新建Country.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MiniWeb
{
public class Country
{
public string CountryCode { get; set; }
public string CountryName { get; set; }
public string CurrencyCode { get; set; }
public string Population { get; set; }
public string Capital { get; set; }
public string ContinentName { get; set; }
public string Continent { get; set; }
public string AreaInSqKm { get; set; }
public string Languages { get; set; }
public string GeonameId { get; set; }
}
}
3.新建接口及其实现
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace MyMiniWeb
{
public interface ICountryRepository
{
IQueryable<Country> All();
IQueryable<Country> AllBy(string filter);
Country Find(string code);
}
}
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace MiniWeb
{
public class CountryRepository : ICountryRepository
{
private static IList<Country> _countries;
public IQueryable<Country> All()
{
EnsureCountriesAreLoaded();
return _countries.AsQueryable();
}
public IQueryable<Country> AllBy(string filter)
{
return string.IsNullOrEmpty(filter)
? All()
: All().Where(m => m.CountryName.ToLower().StartsWith(filter.ToLower()));
}
public Country Find(string code)
{
return All().Where(c => c.CountryCode.Equals(code, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
}
private static void EnsureCountriesAreLoaded()
{
if (_countries == null)
_countries = LoadCountriesFromStream();
}
private static IList<Country> LoadCountriesFromStream()
{
var json = File.ReadAllText("Countries.json");
var contries = JsonConvert.DeserializeObject<Country[]>(json);
return contries;
}
}
}
3.新建Countries.json
[
{
"countryCode": "AD",
"countryName": "Andorra",
"currencyCode": "EUR",
"population": "84000",
"capital": "Andorra la Vella",
"continentName": "Europe",
"continent": "EU",
"areaInSqKm": "468.0",
"languages": "ca",
"geonameId": "3041565"
},
{
"countryCode": "AE",
"countryName": "United Arab Emirates",
"currencyCode": "AED",
"population": "4975593",
"capital": "Abu Dhabi",
"continentName": "Asia",
"continent": "AS",
"areaInSqKm": "82880.0",
"languages": "ar-AE,fa,en,hi,ur",
"geonameId": "290557"
},
{
"countryCode": "AF",
"countryName": "Afghanistan",
"currencyCode": "AFN",
"population": "29121286",
"capital": "Kabul",
"continentName": "Asia",
"continent": "AS",
"areaInSqKm": "647500.0",
"languages": "fa-AF,ps,uz-AF,tk",
"geonameId": "1149361"
},
{
"countryCode": "AG",
"countryName": "Antigua and Barbuda",
"currencyCode": "XCD",
"population": "86754",
"capital": "St. John's",
"continentName": "North America",
"continent": "NA",
"areaInSqKm": "443.0",
"languages": "en-AG",
"geonameId": "3576396"
}
]
4.注册服务
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton<ICountryRepository>(new CountryRepository());
}
5. 配置应用程序
public void Configure(IApplicationBuilder app, IWebHostEnvironment env,ICountryRepository country)
{
app.Run(async (context) =>
{
var query = context.Request.Query["q"];
var listOfCountries = country.AllBy(query).ToList();
var json = JsonConvert.SerializeObject(listOfCountries);
await context.Response.WriteAsync(json);
});
}