2015年2月25日 星期三

Asp.net vnext 簡易示範

      自從微軟推出MVC後,每隔一段時間,就有新版本MVC出現,目前據說最大幅度的變化是MVC 6,微軟已經釋出MVC 6測試版,所以想下戴體驗看看MVC6的改變寫法,也提供相關經驗給有興趣的人參考看看。 

一、     建立Asp.net VNext WebAPI 

步驟1: 新增Asp.net 5 preview Empty 












步驟2: Project.json 手動注入Microsoft.AspNet.DiagnosticsMicrosoft.AspNet.Mvc

 Project.json
{
    "webroot": "wwwroot",
    "version": "1.0.0-*",
    "dependencies": {
        "Microsoft.AspNet.Server.IIS": "1.0.0-beta3",
        "Microsoft.AspNet.Diagnostics": "1.0.0-beta3", //注入Diagnostics
        "Microsoft.AspNet.Mvc": "6.0.0-beta3", //注入 MVC
    },
    "frameworks": {
        "aspnet50": { },
        "aspnetcore50": { }
    },
    "bundleExclude": [
        "node_modules",
        "bower_components",
        "**.kproj",
        "**.user",
        "**.vspscc"
    ],
    "exclude": [
        "wwwroot",
        "node_modules",
        "bower_components"
    ]
}

 Starup.cs
using System;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.Http;
using Microsoft.Framework.DependencyInjection;
namespace WebApplication1
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();  //加上這一行
        }

        public void Configure(IApplicationBuilder app)
        {
            app.UseWelcomePage();//加上這一行
            app.UseMvc();//加上這一行
        }
    }
}

 asp.net Vnext預設畫面











步驟3:加入model

請手動增加model資料夾,並新增class

  public class TodoItem
    {
        public int Id
        {
            get; set;
        }
      
        [Required]
        public string Title
        {
            get; set;
        }
        public bool IsDone
        {
            get; set;
        }
    }
   
步驟4:加入Controller

 [Route("api/[controller]")] : 呼叫web API的網址http://.../api/controlllerName

namespace WebApplication1.Controllers
{
    [Route("api/[controller]")]
    public class HomeController : Controller
    {
        static readonly List<TodoItem> _items = new List<TodoItem>()
        {
            new TodoItem { Id = 1, Title = "First Item" }
        };

        [HttpGet]
        public IEnumerable<TodoItem> GetAll()
        {
            return _items;
        }
        [HttpGet("{id:int}", Name = "GetByIdRoute")]
        public IActionResult GetById(int id)
        {
            var item = _items.FirstOrDefault(x => x.Id == id);
            if (item == null)
            {
                return HttpNotFound();
            }
            return new ObjectResult(item);
        }                
    }
}

 步驟5: 在專案à右鍵à properties

可以自訂port


步驟6:下戴Google Chrome 的應用程式Postman



因為Web API 只會對應協定(POST/GET/DELETE/PUT/PATCH…等等),再搭配參數輸入,所以呼叫/api/Home 會呼叫到GetAll()(HttpGet 協定 不帶參數)

[HttpGet]
        public IEnumerable<TodoItem> GetAll()
        {
            return _items;
        }

   
輸入網址: http://localhost:5000/api/Home/1







呼叫/api/Home/1 會呼叫httpGet協定, 並且有int參數的GetById的函式

  [HttpGet("{id:int}", Name = "GetByIdRoute")]
        public IActionResult GetById(int id)
        {
            var item = _items.FirstOrDefault(x => x.Id == id);
            if (item == null)
            {
                return HttpNotFound();
            }
            return new ObjectResult(item);
        }
  
參考資料:

沒有留言:

張貼留言