0.结构:
1.API
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; namespace LJS.Api { public class Program { public static void Main(string[] args) { CreateWebHostBuilder(args).Build().Run(); } public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args).UseIIS().UseStartup<Startup>(); } }
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using LJS.AppSrv; using LJS.Domains; using LJS.Utils; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.HttpsPolicy; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Newtonsoft.Json.Serialization; using Swashbuckle.AspNetCore.Swagger; namespace LJS.Api { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; AppSetting.SetAppSetting(Configuration.GetSection("ConfigurationInfo")); } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { //services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options => { options.SerializerSettings.ContractResolver = new DefaultContractResolver(); }); ; // services.AddMvcCore().AddApiExplorer(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "信息API", Version = "v1", Contact = new Contact() { Name = "作者", Email = "电子邮箱" } }); var basepath = System.AppContext.BaseDirectory; string[] arr = new string[] { "LJS.AppSrv.xml", "LJS.Api.xml" }; foreach (var item in arr) { var xmlpath = Path.Combine(basepath, item); c.IncludeXmlComments(xmlpath); } }); string[] urls = Configuration.GetSection("AllowedCors").Value.Split(","); services.AddCors(c => c.AddPolicy("AllowAllOrigin", bulid => { bulid.WithOrigins(urls).AllowAnyMethod().AllowAnyHeader().AllowCredentials(); })); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); // Enable middleware to serve generated Swagger as a JSON endpoint. app.UseCors("AllowAllOrigin"); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "API V1"); //c.ShowRequestHeaders(); }); //app.UseMvc(routes => //{ // routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); //}); app.UseMvc(); } } }
2.设置API为启动项目
3.在VS打开 程序包管理控制台,选择DOMAINS项目
4.运行
Add-Migration "PartXXMigs" -OutputDir "Migrations" -Context "DbXXContext"
Update-Database "PartXXMigs" -Context "DbXXContext"
5.api - launchSettings.json
{ "$schema": "http://json.schemastore.org/launchsettings.json", "iisSettings": { "windowsAuthentication": false, "anonymousAuthentication": true, "iisExpress": { "applicationUrl": "http://localhost:62806", "sslPort": 0 } }, "profiles": { "IIS Express": { "commandName": "IISExpress", "launchBrowser": true, "launchUrl": "swagger", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, "LJS.Api": { "commandName": "Project", "launchBrowser": true, "launchUrl": "swagger", "applicationUrl": "http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } }