2. 今日主題
使用 ASP.NET 5 實戰開發雲端應用程式
01 | ASP.NET 5 簡介 05 | ASP.NET 5 與 Azure Web App
02 | ASP.NET 5 專案範本介紹
03 | ASP.NET 5 執行生命週期
04 | Visual Studio 2015 開發工具支援
12. Web API 範本 (1/4)
• ASP.NET 5 統一 MVC 與 Web API 的開發模型
[Route("api/[controller]")]
public class ValuesController : Controller
{
// GET: api/values
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
14. Web API 範本 (3/4)
• Web 應用程式的進入點:Startup 類別 ( Startup.cs )
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// 取消以下註解可讓你的 Web API 相容於第二版
// 你需要加入 Microsoft.AspNet.Mvc.WebApiCompatShim 套件
// services.AddWebApiConventions();
}
15. Web API 範本 (4/4)
• Web 應用程式的進入點:Startup 類別 ( Startup.cs )
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Configure the HTTP request pipeline.
app.UseStaticFiles();
// Add MVC to the request pipeline.
app.UseMvc();
// Add the following route for porting Web API 2 controllers.
// routes.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}");
}
16. Web Site 範本 (1/5)
• ASP.NET 5 統一 MVC 與 Web API 的開發模型
public class HomeController : Controller
{
public IActionResult Get()
{
return View();
}
18. Web Site 範本 (3/5)
• Web 應用程式的進入點:Startup 類別 ( Startup.cs )
public Startup(IHostingEnvironment env)
{
var configuration = new Configuration()
.AddJsonFile("config.json")
.AddJsonFile($"config.{env.EnvironmentName}.json", optional: true);
if (env.IsEnvironment("Development"))
{
configuration.AddUserSecrets();
}
configuration.AddEnvironmentVariables();
Configuration = configuration;
}
19. Web Site 範本 (4/5)
• Web 應用程式的進入點:Startup 類別 ( Startup.cs )
public void ConfigureServices(IServiceCollection services)
{
// Add Application settings to the services container.
services.Configure<AppSettings>(Configuration.GetSubKey("AppSettings"));
// Add MVC services to the services container.
services.AddMvc();
// services.AddWebApiConventions();
}
20. Web Site 範本 (5/5)
• Web 應用程式的進入點:Startup 類別 ( Startup.cs )
public void Configure(IApplicationBuilder app)
{
// 設定 HTTP 要求管線 (順序很重要)
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action}/{id?}",
defaults: new { controller = "Home", action = "Index" });
});
}
41. ASP.NET 5 與 AZURE WEB APP
ASP.NET 5 and Azure Web App
42. Web 單鍵發行 (Web One Click Publish)
• 單鍵發行三部曲
– 下載發行設定檔 (Publish Profile)
– 從 Visual Studio 2013 匯入發行設定檔
– 透過 Kudu 查看部署的檔案內容
http://sitename.scm.azurewebsites.net/
43. 在 Azure Web App 設定環境變數
• Web 應用程式 設定 應用程式設定
• 索引鍵:ASPNET_ENV (此名稱之後會改變)
• 值:
– Development
– Staging
– Production
44. ASP.NET 5 相關連結
• ASP.NET 5 官網
http://www.asp.net/vnext
• ASP.NET 5 官方文件
http://docs.asp.net/
• ASP.NET 5 公告規格重大變更的地方
https://github.com/aspnet/Announcements/issues
• ASP.NET 5 開放原始碼首頁
https://github.com/aspnet/home
• ASP.NET 5 Wiki Documentation
https://github.com/aspnet/Home/wiki
45. 聯絡資訊
• The Will Will Web
記載著 Will 在網路世界的學習心得與技術分享
– http://blog.miniasp.com/
• Will 保哥的技術交流中心 (臉書粉絲專頁)
– http://www.facebook.com/will.fans
• Will 保哥的噗浪
– http://www.plurk.com/willh/invite
Editor's Notes
ASP.NET 5 Community Standuphttps://goo.gl/eNkphF
Jabbr.net for ASP.NET vNext
https://jabbr.net/#/rooms/AspNetvNext