ASP.NET Core Create New Project Template


介紹如何新增一個專案樣板(Project Template)並新增至 .NET CLI 中方便我們重複使用

header

建立一個新專案往往都會有些習慣性會加入的套件或是修改,如果每次都要重複一樣的步驟是很煩人的.如網站開發常常會用到的資料儲存的套件,或是API開發會需要使用到的說明與測試套件等.

而在.NET Core的部份可透過.NET CLI幫忙建立客製化的專案樣板,達到輕鬆重複建立同類型新專案環境的需求.而在.NET Core的部份可透過.NET CLI幫忙建立客製化的專案樣板,達到輕鬆重複建立同類型新專案環境的需求.

這邊我們將示範建立一個Web API專案,並加入 Swashbuckle.AspNetCore , MongoDB.DriverStackExchange.Redis.並將這樣的專案設定為的新樣板,透過新樣板在建立新專案.

Create Customize Project

這邊我們先用Web API的樣板幫我們成立新專案並加入 Swashbuckle.AspNetCore , MongoDB.DriverStackExchange.Redis :

mkdir app || cd app
dotnet new webapi
dotnet add package Swashbuckle.AspNetCore
dotnet add package MongoDB.Driver
dotnet add package StackExchange.Redis
dotnet restore

可以看到我們專案有載入以下packages:

structure_dependency

這邊我們先將Swagger的環境設定好:

Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

using Swashbuckle;
using Swashbuckle.AspNetCore.Swagger;

namespace NetCoreTemplateDemo
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services.
            services.AddMvc();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseMvc();
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
            });
        }
    }
}

接著將網站運行起來

dotnet run

我們就可以透過 http://localhost:5000/swagger/#/ 看到以下畫面:

swagger

到此專案的框架已經準備好,接著我們透過 .NET CLI 幫我們建立樣板

Build New Customize Project Template

開始前先看一下等等要新增資料夾的結構:

structure_folder

dotnet new -i


作者: Blackie
版權聲明: 本站所有文章除特別聲明外,均採用 CC BY 4.0 許可協議。轉載請註明來源 Blackie !
 上一篇
ASP.NET Core MVC play with Google App Engine - Flexible Environment with Custom Runtime ASP.NET Core MVC play with Google App Engine - Flexible Environment with Custom Runtime
先前為大家介紹了GAE的 Standard Environment ,這次將介紹如何實際將ASP.NET Core MVC專案放置在GAE的 Flexible Environment 環境運行。
2017-04-28
下一篇 
Quick Implement FullScreen and other controls on HLS.JS with Plyr.JS Quick Implement FullScreen and other controls on HLS.JS with Plyr.JS
使用 Plyr.JS 快速的幫HTML5的 video tag支援更多功能並輕鬆客製化介面
2017-04-26
  目錄