About how to create a Core MVC / API can not say here, some time ago the blog have said:
1. Project will generate two classes shown in FIG Startup Program class Class
2. Startup class initial content
- public void ConfigureServices(IServiceCollection services)
- {
- // This method is called run-time. Use this method to add services to the container.
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- // This method is called run-time. Use this method to configure the HTTP request pipeline
- }
2.1 ConfigureServices method to add MVC service / EF / Add Custom Service
- publicvoidConfigureServices(IServiceCollection services)
- {
- // inject MVC service
- services.AddMvc();
- // Add the EF service you can add multiple use more of the plurality EF library
- //services.AddEntityFrameworkSqlServer().AddDbContext<EFDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServer")));
- // services.AddEntityFrameworkSqlServer().AddDbContext<EFLogDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("SqlServerLog")));
- services.AddMvc();
- // add a custom service: See IServiceCollection
- }
2.2 Configure using the MVC routing method / static file / error logs, etc.
About IHostingEnvironment (https://msdn.microsoft.com/zh-cn/library/system.web.hosting.hostingenvironment.aspx)
- // redefine IHostingEnvironment
- public IHostingEnvironment HostingEnvironment { get; }
- // This method is called run-time. Use this method to configure the HTTP request pipeline
- public void Configure(IApplicationBuilder app)
- {
- // determine whether the current operating environment is Microsoft if it returns true
- // If you want to determine the name of the environment other operating environments such as Linux can env.IsEnvironment ( "environmentname") to validate ignore case
- if (HostingEnvironment.IsDevelopment())
- {
- // fetch error information to error messages generated HTML
- // writes about on this and other error when processing in detail ****************************
- app.UseDeveloperExceptionPage();
- }
- else
- {
- // custom error messages help page
- app.UseExceptionHandler("/Home/Error");
- }
- // has been rewritten
- //if (env.IsDevelopment())
- //{
- // app.UseDeveloperExceptionPage();
- //}
- // Use the default MVC route
- app.UseMvcWithDefaultRoute();
- App . UseMvc (); // use the MVC pipeline path may configure the routing and other operations where
- //app.UseMvc(
- // routes =>
- // {
- // routes.MapRoute(
- // name: "User",
- // template: "{controller}/{action}/{id?}",
- // defaults: new { controller = "User", action = "Index" });
- // });
- //app.UseMvc(routes =>
- //{
- // routes.MapRoute(
- // name: "default",
- // template: "{controller}/{action=Index}/{id?}");
- //});
- }
2. Program class initial content
- public class Program
- {
- public static void Main(string[] args)
- {
- BuildWebHost(args).Run();
- }
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseStartup<Startup>()
- .Build();
- }
2.1 Startup implementation does not rely initiator may be constructed directly in the Program class expanded configuration, the configuration, expansion, log
- public class Program
- {
- public static IServiceCollection services { get; set; }
- public static IHostingEnvironment HostingEnvironment { get; set; }
- public static void Main(string[] args)
- {
- BuildWebHost(args).Run();
- }
- public static IWebHost BuildWebHost(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- // build extensions, configuration, configuration, expansion, logging, ILoggerFactory
- .ConfigureAppConfiguration((WebHostBuilderContext, config) =>
- {
- HostingEnvironment = WebHostBuilderContext.HostingEnvironment;
- })
- .ConfigureServices((IServiceCollection, services) =>
- {
- services.AddMvc();
- })
- .Configure(app =>
- {
- if (HostingEnvironment.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- else
- {
- app.UseExceptionHandler("/Error");
- }
- // Use the default MVC route
- app.UseMvcWithDefaultRoute();
- // use the static file
- app.UseStaticFiles("");
- // Configure routing
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller}/{action=Index}/{id?}");
- });
- })
- // be replaced startup items
- // .UseStartup<Startup>()
- .Build();
- }
Deficiencies, please point out to learn from each other