如果想修改說明
可以點入專案 Repositories
左邊有 Settings 選項
請點擊
進入 Settings
右邊就會列出可以修改的內容
<Target Name="ExecuteBatAfterPublish" AfterTargets="AfterPublish">
<Exec Command="C:\Batch\WinToUbuntu.bat" WorkingDirectory="$(publishUrl)" />
</Target>
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
#region Register the MiniProfiler services
services.AddMiniProfiler(options => options.RouteBasePath = "/profiler");
#endregion Register the MiniProfiler services
....
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
#region Register the MiniProfiler
app.UseMiniProfiler();
#endregion Register the MiniProfiler
}
[HttpGet]
public IEnumerable Get()
{
string url1 = string.Empty;
string url2 = string.Empty;
using (MiniProfiler.Current.Step("Get方法"))
{
using (MiniProfiler.Current.Step("準備數據"))
{
using (MiniProfiler.Current.CustomTiming("SQL", "SELECT * FROM Config"))
{
// 模擬一個SQL查詢
Thread.Sleep(500);
url1 = "https://www.baidu.com";
url2 = "https://www.sina.com.cn/";
}
}
using (MiniProfiler.Current.Step("使用從數據庫中查詢的數據,進行Http請求"))
{
using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url1))
{
var client = new WebClient();
var reply = client.DownloadString(url1);
}
using (MiniProfiler.Current.CustomTiming("HTTP", "GET " + url2))
{
var client = new WebClient();
var reply = client.DownloadString(url2);
}
}
}
return new string[] { "value1", "value2" };
}
using NSwag.AspNetCore;
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
#region Register the Swagger services
// Register the Swagger services
services.AddSwaggerDocument();
#endregion Register the Swagger services
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
#region Register the Swagger generator and the Swagger UI middlewares
// Register the Swagger generator and the Swagger UI middlewares
app.UseSwagger();
app.UseSwaggerUi3();
#endregion Register the Swagger generator and the Swagger UI middlewares
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}");
});
}
/// <summary>
/// 簡單的 Controller 範例
/// </summary>
/// <summary>
/// 異步函式
/// </summary>
static async void AsyncFun()
{
// 裡面要有 await 關鍵字
// await 後面要接回傳 Task 的函式
// 因為回傳 Task 簡稱他為異步任務
List<formatter> f_ = new List<formatter>{
new Formatter("「異步任務」", Color.Red)};
Print("執行 {0} 『開始』", Color.Yellow, f_.ToArray());
bool ret_ = await DelayFun();
Print("執行 {0} 『結束』", Color.Yellow, f_.ToArray());
}
/// <summary>
/// 異步任務
/// </summary>
/// <returns>true</returns>
static Task<bool> DelayFun()
{
return Task.Run(() =>
{
List<Formatter> f_ = new List<Formatter>{
new Formatter("等待五秒", Color.YellowGreen)};
Print("{0} 開始", Color.Red, f_.ToArray());
Thread.Sleep(5000);
Print("{0} 結束", Color.Red, f_.ToArray());
return true;
});
}
static void Main(string[] args)
{
....
// 異步函式
List<Formatter> f0_ = new List<Formatter>{
new Formatter("「異步函式」", Color.Red)};
Print("執行 {0} 開始", Color.Green, f0_.ToArray());
AsyncFun();
Print("執行 {0} 結束", Color.Green, f0_.ToArray());
....
}
public class CommitID
{
private static string EnvironmentVariable
{
get
{
string sPath = Environment.GetEnvironmentVariable("Path");
var result = sPath.Split(';');
for (int i = 0; i < result.Length; i++)
{
if (result[i].Contains(@"Git\cmd"))
{
sPath = result[i];
}
}
return sPath;
}
}
public static void GetCommitID()
{
string gitPath = System.IO.Path.Combine(EnvironmentVariable, "git.exe");
Console.WriteLine($"環境路徑:{gitPath}");
Process p = new Process();
p.StartInfo.FileName = gitPath;
p.StartInfo.Arguments = "rev-parse HEAD";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.OutputDataReceived += OnOutputDataReceived;
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
}
private static void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (e != null && !string.IsNullOrEmpty(e.Data))
{
ID = e.Data;
Console.WriteLine(e.Data);
}
}
public static string ID { get; set; } = "";
}
<#@ template debug="false" hostspecific="true" language="C#" #>
<#@ import namespace="System.IO" #>
<#@ assembly name="$(SolutionDir)\CommitID\bin\Debug\netstandard2.0\MyLibrary.Git.dll" #>
<#@ import namespace="MyLibrary.Git" #>
<#@ assembly name="C:\Program Files\dotnet\sdk\2.1.700-preview-009597\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll" #>
<#@ output extension=".cs" #>
<#
int major = 0;
int minor = 0;
int build = 0;
int revision = 0;
try
{
using(var f = File.OpenText(Host.ResolvePath("SharedInfo.cs")))
{
// 取值做 +1 使用
//string maj = f.ReadLine().Replace("//","");
//string min = f.ReadLine().Replace("//","");
//string b = f.ReadLine().Replace("//","");
//string r = f.ReadLine().Replace("//","");
// 轉成日期
string maj = DateTime.Now.Year.ToString();
string min = DateTime.Now.Month.ToString();
string b = DateTime.Now.Day.ToString();
string r = DateTime.Now.Minute.ToString() + "0" + DateTime.Now.Second.ToString();
major = int.Parse(maj);
minor = int.Parse(min);
build = int.Parse(b);
revision = int.Parse(r) + 1;
}
}
catch
{
major = 1;
minor = 0;
build = 0;
revision = 255;
}
// 呼叫自己個函式庫
CommitID.GetCommitID();
string gitver = CommitID.ID;
#>
//<#= major #>
//<#= minor #>
//<#= build #>
//<#= revision #>
//<#= gitver #>
//
// This code was generated by a tool. Any changes made manually will be lost
// the next time this code is regenerated.
//
using System.Reflection;
[assembly: AssemblyFileVersion("<#= major #>.<#= minor #>.<#= build #>.<#= revision #>")]
[assembly: AssemblyTitle("<#= gitver #>")]
<#@ assembly name="$(SolutionDir)\CommitID\bin\Debug\netstandard2.0\MyLibrary.Git.dll" #>
<#@ import namespace="MyLibrary.Git" #>
<#@ assembly name="C:\Program Files\dotnet\sdk\2.1.700-preview-009597\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll" #>
using Swashbuckle.AspNetCore.Swagger;
設定 Startup.cs 的 ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
#region 註冊 Swagger
// 註冊 Swagger
services.AddSwaggerGen(c =>
{
c.SwaggerDoc(
// name: 攸關 SwaggerDocument 的 URL 位置。
name: "v1",
// info: 是用於 SwaggerDocument 版本資訊的顯示(內容非必填)。
info: new Info
{
Title = "標題",
Version = "版本號 1.0.0",
Description = "說明",
TermsOfService = "無",
Contact = new Contact
{
Name = "強尼 John Wu",
Url = "https://blog.johnwu.cc"
},
License = new License
{
Name = "西西 CC BY-NC-SA 4.0",
Url = "https://creativecommons.org/licenses/by-nc-sa/4.0/"
}
}
);
// 為 Swagger JSON and UI設置xml文檔註釋路徑
var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//獲取應用程序所在目錄(絕對,不受工作目錄影響,建議採用此方法獲取路徑)
var xmlPath = Path.Combine(basePath, "Swagger.xml");
c.IncludeXmlComments(xmlPath);
});
#endregion 註冊 Swagger
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
#region 註冊 Swagger
// 註冊 Swagger
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint(
// url: 需配合 SwaggerDoc 的 name。 "/swagger/{SwaggerDoc name}/swagger.json"
url: "/swagger/v1/swagger.json",
// name: 用於 Swagger UI 右上角選擇不同版本的 SwaggerDocument 顯示名稱使用。
name: "RESTful API v1.0.0"
);
});
#endregion 註冊 Swagger
app.UseMvc(routes =>
{
routes.MapRoute(name: "default", template: "{controller}/{action}/{id?}");
});
}
程式開啟後,輸入網址 http://localhost:port/swagger
{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
}
}
建立存放 appsetting.json 的類別 MySetting.cs
public class MySetting
{
public Logging Logging { get; set; }
}
public class Logging
{
public LogLevel LogLevel { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
}
在 Startup.cs 的 ConfigureServices 中註冊
public void ConfigureServices(IServiceCollection services)
{
services.Configure<mysetting>(Configuration);
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
在 Controller 中使用 IOptions <mysetting>注入
public class HomeController : Controller
{
private IOptions<mysetting> myOption;
public HomeController(IOptions<mysetting> _option)
{
myOption = _option;
}
}
1>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild
\15.0\Bin\Microsoft.Common.CurrentVersion.targets(2110,5)
: warning MSB3276: 在同一個相依組件的不同版本之間發現衝突。
請將專案檔中的 "AutoGenerateBindingRedirects" 屬性設為 true。
如需詳細資訊,請參閱 http://go.microsoft.com/fwlink/?LinkId=294190。
// '<'與'>'被改成全形了,記得改回來半形
<autogeneratebindingredirects>true</autogeneratebindingredirects>
警告就消失了
```csharp
public class MyLogger : ILogger
{
public void Print(string msg, Color color)
{
Log.Print(msg, color);
}
}
```
這樣就能順眼的顯示 C# 的程式碼了,如下圖:
參考文章: 覺得 Google 的 Blogger 不太順手?透過 HTML 的 iframe 移花接木 HackMD