﻿
{"id":8434,"date":"2018-07-04T18:59:47","date_gmt":"2018-07-04T18:59:47","guid":{"rendered":"http:\/\/www.gencayyildiz.com\/blog\/?p=8434"},"modified":"2018-07-04T18:59:47","modified_gmt":"2018-07-04T18:59:47","slug":"c-ile-web-api-tetikleme","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/c-ile-web-api-tetikleme\/","title":{"rendered":"C# \u0130le Web API Tetikleme"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Son zamanlarda Asp.NET MVC Web API \u00fczerine odaklanm\u0131\u015f bulunmaktay\u0131m. Dolay\u0131s\u0131yla bu konuya dair ak\u0131llara gelebilecek t\u00fcm sorulara cevap verebilmek i\u00e7in bir \u00e7aba i\u00e7erisine girmi\u015f bulunmaktay\u0131m. Velhas\u0131l bu i\u00e7eri\u011fimizde, C# ile Web API&#8217;nin nas\u0131l tetiklendi\u011fini inceleyecek ve de\u011ferlendirece\u011fiz.<\/p>\n<p>Varsayal\u0131m, elimizde a\u015fa\u011f\u0131daki gibi bir Web API olsun.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    public class PersonelController : ApiController\r\n    {\r\n        NorthwindEntities db = new NorthwindEntities();\r\n        public List&lt;Personel&gt; GetAll() =&gt; db.Personeller.OrderByDescending(p =&gt; p.PersonelID).Select(p =&gt; new Personel\r\n        {\r\n            Adi = p.Adi,\r\n            SoyAdi = p.SoyAdi,\r\n            Unvan = p.Unvan,\r\n            PersonelID = p.PersonelID\r\n        }).ToList();\r\n        public Personel GetSingle(int id) =&gt; db.Personeller.FirstOrDefault(p =&gt; p.PersonelID == id);\r\n        public IHttpActionResult PostAdd(Personel personel)\r\n        {\r\n            if (personel != null)\r\n            {\r\n                db.Personeller.Add(personel);\r\n                db.SaveChanges();\r\n                return Ok(personel);\r\n            }\r\n            return Ok(&quot;Personel eklenirken bir hata olu\u015ftu.&quot;);\r\n        }\r\n        public IHttpActionResult PutUpdate(Personel personel)\r\n        {\r\n            if (personel != null)\r\n            {\r\n                Personeller oldPersonel = db.Personeller.FirstOrDefault(p =&gt; p.PersonelID == personel.PersonelID);\r\n                if (oldPersonel != null)\r\n                {\r\n                    oldPersonel.GetType().GetProperties().ToList().ForEach(p =&gt;\r\n                    {\r\n                        if (p.Name != &quot;PersonelID&quot; &amp;&amp; (p.Name == &quot;Adi&quot; || p.Name == &quot;SoyAdi&quot; || p.Name == &quot;Unvan&quot;))\r\n                            p.SetValue(oldPersonel, personel.GetType().GetProperty(p.Name).GetValue(personel));\r\n                    });\r\n                    db.SaveChanges();\r\n                    return Ok(personel);\r\n                }\r\n            }\r\n            return Ok(&quot;Personel g\u00fcncellenirken hata olu\u015ftu.&quot;);\r\n        }\r\n        public IHttpActionResult Delete(int id)\r\n        {\r\n            Personeller personel = db.Personeller.FirstOrDefault(p =&gt; p.PersonelID == id);\r\n            if (personel != null)\r\n            {\r\n                db.Personeller.Remove(personel);\r\n                db.SaveChanges();\r\n                return Ok(personel);\r\n            }\r\n            return Ok(&quot;Personel silinirken bir hata olu\u015ftu.&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<p>\u00d6ncelikle bu Web API&#8217;ye uygun GET ve POST i\u015flemlerinde kullanaca\u011f\u0131m\u0131z modelimizi olu\u015ftural\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Personel\r\n    {\r\n        public int PersonelID { get; set; }\r\n        public string Adi { get; set; }\r\n        public string SoyAdi { get; set; }\r\n        public string Unvan { get; set; }\r\n    }\r\n<\/pre>\n<p>\u015eimdi servis g\u00f6revi g\u00f6recek s\u0131n\u0131f\u0131m\u0131z\u0131 in\u015fa edelim.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class HttpService\r\n    {\r\n        static string url = &quot;http:\/\/localhost:58845\/api\/&quot;;\r\n        static HttpClient client = new HttpClient();\r\n\r\n        public static async Task&lt;string&gt; Get(string method)\r\n        {\r\n            string serviceUrl = $&quot;{url}{method}&quot;;\r\n            using (var r = await client.GetAsync(new Uri(serviceUrl)))\r\n                return await r.Content.ReadAsStringAsync();\r\n        }\r\n        public static async Task&lt;string&gt; Post(string method, Personel personel)\r\n        {\r\n            string serviceUrl = $&quot;{url}{method}&quot;;\r\n            StringContent httpContent = new StringContent(JsonConvert.SerializeObject(personel), Encoding.UTF8, &quot;application\/json&quot;);\r\n            using (HttpResponseMessage response = await client.PostAsync(serviceUrl, httpContent))\r\n            {\r\n                response.EnsureSuccessStatusCode();\r\n                return await response.Content.ReadAsStringAsync();\r\n            }\r\n        }\r\n        public static async Task&lt;string&gt; Put(string method, Personel personel)\r\n        {\r\n            string serviceUrl = $&quot;{url}{method}&quot;;\r\n            StringContent httpContent = new StringContent(JsonConvert.SerializeObject(personel), Encoding.UTF8, &quot;application\/json&quot;);\r\n            using (HttpResponseMessage response = await client.PutAsync(serviceUrl, httpContent))\r\n            {\r\n                response.EnsureSuccessStatusCode();\r\n                return await response.Content.ReadAsStringAsync();\r\n            }\r\n        }\r\n        public static async Task&lt;string&gt; Delete(string method)\r\n        {\r\n            string serviceUrl = $&quot;{url}{method}&quot;;\r\n            using (HttpResponseMessage response = await client.DeleteAsync(serviceUrl))\r\n            {\r\n                return await response.Content.ReadAsStringAsync();\r\n            }\r\n        }\r\n    }\r\n<\/pre>\n<p>Ve son olarak servis s\u0131n\u0131f\u0131n\u0131 kullanarak Web API&#8217;yi tetikleyecek metotlar\u0131m\u0131z\u0131 olu\u015ftural\u0131m ve projemizi \u00e7al\u0131\u015ft\u0131ral\u0131m.<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n    class Program\r\n    {\r\n        static void Main(string&#x5B;] args)\r\n        {\r\n            Console.WriteLine(&quot;GetAll&quot;);\r\n            GetAll();\r\n            Console.WriteLine(&quot;******************&quot;);\r\n            Console.WriteLine(&quot;GetSingle&quot;);\r\n            GetSingle(3);\r\n            Console.WriteLine(&quot;******************&quot;);\r\n            Console.WriteLine(&quot;PostAdd&quot;);\r\n            PostAdd(new Personel { Adi = &quot;Mustafa&quot;, SoyAdi = &quot;Kemal&quot;, Unvan = &quot;Atat\u00fcrk&quot; });\r\n            Console.WriteLine(&quot;******************&quot;);\r\n            Console.WriteLine(&quot;PutUpdate&quot;);\r\n            PutUpdate(new Personel { PersonelID = 1043, Adi = &quot;Mustafa1&quot;, SoyAdi = &quot;Kemal2&quot;, Unvan = &quot;Atat\u00fcrk3&quot; });\r\n            Console.WriteLine(&quot;******************&quot;);\r\n            Console.WriteLine(&quot;Delete&quot;);\r\n            Delete(1061);\r\n            Console.Read();\r\n        }\r\n        static void GetAll()\r\n        {\r\n            string jsonData = HttpService.Get(&quot;personel\/GetAll&quot;).Result;\r\n            List&lt;Personel&gt; personeller = JsonConvert.DeserializeObject&lt;List&lt;Personel&gt;&gt;(jsonData);\r\n            personeller.ForEach(p =&gt; Console.WriteLine($&quot;{p.PersonelID} - {p.Adi} {p.SoyAdi}\\t\\t{p.Unvan}&quot;));\r\n        }\r\n        static void GetSingle(int id)\r\n        {\r\n            string jsonData = HttpService.Get($&quot;personel\/GetSingle\/{id}&quot;).Result;\r\n            Personel personel = JsonConvert.DeserializeObject&lt;Personel&gt;(jsonData);\r\n            Console.WriteLine($&quot;{personel.Adi} {personel.SoyAdi}\\t\\t{personel.Unvan}&quot;);\r\n        }\r\n        static void PostAdd(Personel personel)\r\n        {\r\n            string jsonData = HttpService.Post(&quot;personel\/PostAdd&quot;, personel).Result;\r\n            Personel addPersonel = JsonConvert.DeserializeObject&lt;Personel&gt;(jsonData);\r\n            Console.WriteLine($&quot;{addPersonel.Adi} {addPersonel.SoyAdi}\\t\\t{addPersonel.Unvan}&quot;);\r\n        }\r\n        static void PutUpdate(Personel personel)\r\n        {\r\n            string jsonData = HttpService.Put(&quot;personel\/PutUpdate&quot;, personel).Result;\r\n            Personel addPersonel = JsonConvert.DeserializeObject&lt;Personel&gt;(jsonData);\r\n            Console.WriteLine($&quot;{addPersonel.Adi} {addPersonel.SoyAdi}\\t\\t{addPersonel.Unvan}&quot;);\r\n        }\r\n        static void Delete(int id)\r\n        {\r\n            string jsonData = HttpService.Delete($&quot;personel\/Delete\/{id}&quot;).Result;\r\n            Personel addPersonel = JsonConvert.DeserializeObject&lt;Personel&gt;(jsonData);\r\n            Console.WriteLine($&quot;{addPersonel.Adi} {addPersonel.SoyAdi}\\t\\t{addPersonel.Unvan}&quot;);\r\n        }\r\n    }\r\n<\/pre>\n<p>Bizler burada herhangi bir authentication bulunmaks\u0131z\u0131n \u00e7al\u0131\u015fma ger\u00e7ekle\u015ftirmi\u015f olduk. Bir sonraki i\u00e7eri\u011fimizde C# ile Token Authentication uygulanm\u0131\u015f Web API&#8217;yi nas\u0131l tetikleyece\u011fimizi inceleyece\u011fiz.<\/p>\n<p>O halde sonraki yaz\u0131m\u0131zda g\u00f6r\u00fc\u015fmek \u00fczere diyelim&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Son zamanlarda Asp.NET MVC Web API \u00fczerine odaklanm\u0131\u015f bulunmaktay\u0131m. Dolay\u0131s\u0131yla bu konuya dair ak\u0131llara gelebilecek t\u00fcm sorulara cevap verebilmek i\u00e7in bir \u00e7aba i\u00e7erisine girmi\u015f bulunmaktay\u0131m. Velhas\u0131l bu i\u00e7eri\u011fimizde, C# ile Web API&#8217;nin nas\u0131l&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3223,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,2402],"tags":[2421,2424,2423,2422],"class_list":["post-8434","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-sharp-c","category-web-api","tag-c-ile-web-api-tetikleme","tag-c-web-api","tag-c-web-api-tetikleme","tag-web-api-tetikleme"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8434","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=8434"}],"version-history":[{"count":1,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8434\/revisions"}],"predecessor-version":[{"id":8435,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/8434\/revisions\/8435"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3223"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=8434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=8434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=8434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}