發表文章

目前顯示的是有「ASP.NET MVC」標籤的文章

【C#】MVC WEB API 語法架構

 ASP.NET WEB API架構是以.NET Framework基礎,可用來建置RESTful 【註1】 應用程式,可建置HTTP服務並提供用戶端瀏覽器和行動裝置應用程式使用。 WEB API相較於Web Services和WCF來說,Web Services和WCF的資料內容不易辨識且傳送資料過於肥大,而WEB API提供XML和JSON的傳輸格式,同時可透過HTTP操作方法GET、POST、PUT、DELETE來操作服務,並提供輕量化與跨平台的特性。 本篇透過範例方式建立支援CRUD操作WEB API,在 Controllers加入控制器"具有讀取/寫入 Web Api 2" 之WEB API控制器。 部分WEB API控制器之程式碼(CustomerController.cs僅供參考)︰ using System.Linq;        //引用命名空間-LINQ using prjView.Models;  //引用命名空間-專案名稱.Models public class CustomerController : ApiController         //WEB API必須繼承自ApiController類別。 {      dbCustomerEntities db=new dbCustomerEntities();   //建立存取資料庫類別物件db        //當瀏覽器輸入「http://localhost/api/Customer」,會對此WEB API發出Get請求,Action方法回傳查詢結果            // GET: api/Customer      public List<tCustomer> Get()                         {     ...

【C#】解決MVC WEB API執行,出現404找不到資源

圖片
在MVC專案中新加入WEB API功能,在 Controllers加入控制器"具有讀取/寫入 Web Api 2" , 第一次加入後假設加入"Customer"(範例參考),會跳出一個txt檔。 專案中的 Global.asax.cs 檔案需做調整,才能正常啟用ASP.NET Web API 。 1.新增下列命名空間參考︰   using System.Web.Http;   using System.Web.Routing; 2.Application_Start()方法,加入下方 標示藍色字體 的方法且位置要在AreaRegistration.RegisterAllAreas()之後,RouteConfig.RegisterRoutes(RouteTable.Routes)之前︰ protected void Application_Start() {     AreaRegistration.RegisterAllAreas();     GlobalConfiguration.Configure(WebApiConfig.Register);    // web api擺放位置     RouteConfig.RegisterRoutes(RouteTable.Routes); }   執行畫面(調整前)︰ 執行畫面(調整後)︰

【C#】ADO.NET 常用資料存取用法

程式碼(Sample): using System.Data;                      //使用ADO.NET,引用命名空間 using System.Data.SqlClient;    //使用ADO.NET,引用命名空間 //(方法一)連接SQL Server資料庫檔案 //constr連接字串指定連接dbStudent.mdf資料庫   string constr = @"Data Source= (LocalDB)\MSSQLLocalDB ;" +                                     "AttachDbFilename=|DataDirectory| dbStudent.mdf ;" +                                     "Integrated Security=True";    //(方法二)連接SQL Server資料庫來源  //使用SqlClient進行SQL Server驗證   //string constr = @"Server= localhost ;Database= Student ;uid= sa_test ;pwd= test1234 ;Persist Security Info=False";    //使用SqlClient進行Windows驗證   //string constr = @"Server= localhost ;Database= Student ;Persist Security Inf...