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);
        }
  
參考資料:

2015年2月12日 星期四

如何使用DataReader取值,並以物件陣列LIST傳回資料

目前公司有些專案上仍採用傳統的模式,資料庫甚至使用foxpro,所以之前以DataTable 方式取回OleDbDataReader資料,但c#大多以物件為主軸,所以需要另外再轉換DataTable成物件,如此程式無法更簡潔,所以作者嘗試直接以dataReader依欄位先後順序逐一注入物件屬性,雖然這方式也有缺點,但至少免去轉換型別的痛苦。

CommonDB

  public  static List<T> selectQueryWithListT<T>(string strCommand, bool isShowDeleted) where T:new()
        {          
                List<T> listData = new List<T>();
                foxProConn.ConnectionString = _connstring();                               
                if (foxProConn.State == ConnectionState.Open)
                {
                    foxProConn.Close();
                }
                foxProConn.Open();
                if (isShowDeleted == true)
                {
                    OleDbCommand oCmdMarkDeleted = new OleDbCommand("SET DELETED OFF", foxProConn);
                    oCmdMarkDeleted.ExecuteNonQuery();
                }
                else
                {
                    OleDbCommand oCmdMarkDeleted = new OleDbCommand("SET DELETED ON", foxProConn);
                    oCmdMarkDeleted.ExecuteNonQuery();
                }
                List<OleDbParameter> liParameters = new List<OleDbParameter>();
                OleDbCommand ocmd = foxProConn.CreateCommand();
                ocmd.CommandText = strCommand;
                using (OleDbDataReader oledbReaderObj = ocmd.ExecuteReader())
                {
                    T valueObj = new T(); //產生新物件
                    Type myType = valueObj.GetType(); //取得該物件的型別
//取得該物件的型別有哪些屬性
                    IList<PropertyInfo> props = new List<PropertyInfo>(myType.GetProperties());
                    int i = 0;
                    while (oledbReaderObj.Read())
                    {
                        T newData = new T(); //重新產生新物件
                        foreach (PropertyInfo prop in props)
                        {
                                prop.SetValue(newData, oledbReaderObj[i]); //逐一填入OledbReader欄位值
                                i++;
                        }
                        listData.Add(newData);
                        i = 0;
                    }
                }
                foxProConn.Close();
                return listData;         
     }


entryDoctor
  public class entryDoctor:commonDB
    {
        public string strDoctorID;  //醫師代號
        public string strDoctorName; //醫師姓名
        public string Gmail; //gmail
        public string Gpass; //gpass
}


呼叫函式:

public List<entryDoctor> listAllEmployeeWithT()
        {          
          int intToday = Convert.ToInt32(DateTime.Today.ToString("yyyyMMdd"));
          intToday -= 19110000;
          string strDoctorCommand = "SELECT 醫師代號,醫師姓名,gmail,gpass FROM DOCTOR WHERE 職務別 LIKE '%醫師%' AND (離職日期 > '" + intToday.ToString() + "' OR 離職日期 =='') ORDER BY 醫師代號";
          List<entryDoctor> liDoctors = commonDB.selectQueryWithListT<entryDoctor>(strDoctorCommand, false);
          return liDoctors;
       }


    如果需傳回其他型別的物件,只需更換型別名稱,非常有彈性的作法,但需要注意SQL欄位的先後順序和物件屬性是否一致,否則就對應到錯誤的欄位, 以上作法僅供參考~~~~ 另外foxpro 也有提供entity framework,也可以請有心人嘗試看看。

c#參考資料:
http://blog.goyello.com/2013/01/30/6-more-things-c-developers-should-not-do/