發表文章

目前顯示的是 5月, 2024的文章

【C#】CRUD操作WEB API 服務

圖片
 延續先前Blog︰ 【C#】MVC WEB API 語法架構 ,介紹的範例實作呼叫WebApi進行非同步新增、讀取、修改、刪除。 在 Controllers加入控制器"MVC 5 控制器 - 空白" 之MVC控制器。 部分MVC控制器之程式碼(Home8Controller.cs僅供參考)︰ public class Home8Controller : Controller {     // GET: Home8     public ActionResult Index2 ()     {         return View();     } } 部分View之程式碼(Index2.cshtml僅供參考)︰ <!-- 引用Jquery 必要條件--> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> <!-- 建議可放置MVC專案預設的版面配置頁 --> @{     ViewBag.Title = "呼叫WebApi進行非同步新增、修改、刪除";  } <script>     $(document).ready(function () {         var apiurl = "http://localhost:49580/api/Customer" ;    //呼叫web api         $("#btnCreate").on("click", fnCreate );    //按下新增按鈕,呼叫fnCreate函式         $("#btnEdit").on("click", fnEdit );        ...

【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); }   執行畫面(調整前)︰ 執行畫面(調整後)︰

【AJAX】用JQuery寫法實作資料介接JSON

圖片
使用JQuery作法,從農業部資料開放平台透過資料介接方式,取得「農村地方美食小吃特色料理」的JSON資料,並呈現在頁面上。 JavaScript(參考): <!-- 引用Jquery 必要條件--> <script src="~/Scripts/jquery-1.10.2.min.js"></script> <script src="~/Scripts/bootstrap.min.js"></script> <script>     $(document).ready(function () {              //整份文件載入後才執行         var dataurl = " https://data.moa.gov.tw/Service/OpenData/ODwsv/ODwsvTravelFood.aspx ";    //存放伺服器位置或json檔         $.ajax({                    //呼叫$.ajax()函式             url: dataurl ,          //指定伺服器位置或json檔             type: 'GET',         //指定伺服器要執行的動作             success: function ( data ) {      //存取成功會執行success函式             ...

【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...