🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# Athena / web service yazımı rest ile - Web Servis Bilgilerini Gösterme

 

1-) C# Athena -  web service yazımı rest ile -> Web Servis Bilgilerini Gösterme

web service yazımı rest ile -> Web Servis Bilgilerini Gösterme

1-) Refarances -> Manage Nuget pacgakes-> Brows sekmesinden ara -> Webapi helppage tam açılımı (Microsoft.AspNET.Webapi.HelpPage) -> yükle

2-) Projende -> Global.asax -> aşağıdaki gibi düzelt

protected void Application_Start()

        {

            AreaRegistration.RegisterAllAreas();

            GlobalConfiguration.Configure(WebApiConfig.Register);

        }

çağırırken -> http://localhost:60308/help  -> şeklinde çağır

3-) help kısmındaki descraption(Açıklama alanına değer eklemek için)

Projende ->Areas->HelpPage-App_Start-HelpPageConfig.cs

  public static void Register(HttpConfiguration config) ->           

//config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/XmlDocument.xml")));

 

-->Kısmını -> yukarıdaki yorum satırını kaldır ve şöyle yap ->

            config.SetDocumentationProvider(new XmlDocumentationProvider(HttpContext.Current.Server.MapPath("~/App_Data/RestWebService.xml")));

--> RestWebService(Projenin ismi demek)

--> Sonra Controllere git -> personController.cs -> örn ->  // GET: api/person -> yazan kısmın üzerindeki boşluğa gel ve 3 tane / yap -> otomatik şöyle olacak ->  

  /// <summary>

            /// Açıklamayı buraya gir

            /// </summary>

            /// <returns></returns>

-> Sonra projene sağ tık -> properties -> Build -> XML dokumantation file seçeneğini işaretle

-> sonra -> Build Event -> Post-build event .. -> copy "$(SolutionDir)RestWebService\bin\RestWebService.xml" "$(ProjectDir)\App_Data\RestWebService.xml" -> şeklinde ayarla buradaki RestWebService(Projenin ismi)

4-) class aşağıda

using System;

using System.Collections;

using System.Collections.Generic;

using System.Net;

using System.Net.Http;

using System.Web.Http;

using RestWebService.Models;

/*

 DİKKAT

 POST DİYORSA POST GONDERECEKSIN

 GET DİYORSA GET

 DELETE DİYORSA DELETE

 PUT DIYORSA PUT

     */

// http://localhost:60308/help   -> WebService hakkında bilgi verir -> Nugetten ekledim -> Webapi.helppage

namespace RestWebService.Controllers {

    public class personController : ApiController {

        //// GET: api/person

        //public IEnumerable<string> Get() {

        //    return new string[] { "value1", "value2" };

        //}

 

            /// <summary>

            /// Açıklamayı buraya gir

            /// </summary>

            /// <returns></returns>

        // GET: api/person  --> yukarıdaki yorum satırınıda incelersin

        public ArrayList Get() {

            ArrayList a=new ArrayList(); // sınıfında listesini yapıp geri donebilirsin

            a.Add("Ramazan");

            a.Add("haber");

 

            return a;

        }

 

        /// <summary>

        /// id ye göre

        /// </summary>

        /// <returns></returns>

        // GET: api/person/5

        public Person Get(int id) {

            Person person = new Person();

            person.id = 1;

            person.ad = "Ramazan";

            person.soyad = "HABER";

            return person;

        }

 

        /// <summary>

        /// id ye göre

        /// </summary>

        /// <returns></returns>

        // GET: api/person/5

        public Person Get(int id,int adi) {  // --> http://localhost:60308/api/person/5?"3"

            Person person = new Person();

            person.id = 1;

            person.ad = "Ramazan";

            person.soyad = "HABER";

            return person;

        }

 

 

        //// POST: api/person

        //public void Post([FromBody]Person value) {

        //   Person yeni = new Person();

        //    yeni.ad= value.ad;

 

        //}

 

        /*

         {"id":1,"ad":"Ramazan","soyad":"HABER"}  --> gibisinden post yapıcan personel sınıfı oluşturup

         */

 

        /// <summary>

        /// personellere post yap

        /// </summary>

        /// <returns></returns>

        // POST: api/person  --> yukarıdaki yorum satırınıda incele bunuda

        public HttpResponseMessage Post([FromBody]Person value) { // http://localhost:60308/api/person/3 şeklinde dinamik nasıl çağıracagını söyler yani hem post yapar hem Request cevap döner

            Person yeni = new Person();

            yeni.ad = value.ad;

 

            HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created); // oluşturuldu mesajını alırsın

            response.Headers.Location = new Uri(Request.RequestUri, "person/3"); // buradaki 3 dinamik id'si olucak ve yukarıdaki metodda api/person/3 dediğimizde bu gelecek

            return response;

        }

 

        // PUT: api/person/5    ---> http://localhost:60308/api/person/5 --> buradaki 5 aşağıdaki id'ye denk gelir -> Put olarak Person json olak gondeceksin

        public HttpResponseMessage Put(int id, [FromBody]Person value) { // güncelleme yapacan --> id'ye göre personeli

            HttpResponseMessage response; // oluşturuldu mesajını alırsın

 

            if (id == 3) { // burada eğer id varsa sil demek ona gore duzenlersın

                response = Request.CreateResponse(HttpStatusCode.NoContent); // bulundu ve silindi

            } else { // eğer gelen id bulunamazsa

                response = Request.CreateResponse(HttpStatusCode.NotFound); // bulunamadı

            }

            //response.Headers.Location = new Uri(Request.RequestUri, "person/3");  

            return response;

        }

 

        //// DELETE: api/person/5

        //public void Delete(int id) { // id'ye göre sileceksin

        //}

 

        // DELETE: api/person/5   --> YUKARIDAKİ YORUM SATIRINIDA INCELE

        public HttpResponseMessage Delete(int id) { // id'ye göre sileceksin

            HttpResponseMessage response ; // oluşturuldu mesajını alırsın

 

            if (id==3) { // burada eğer id varsa sil demek ona gore duzenlersın

                response = Request.CreateResponse(HttpStatusCode.NoContent); // bulundu ve silindi

            }else { // eğer gelen id bulunamazsa

                response = Request.CreateResponse(HttpStatusCode.NotFound); // bulunamadı

            }

            //response.Headers.Location = new Uri(Request.RequestUri, "person/3");  

            return response;

        }

    }

}

 2021 Ocak 18 Pazartesi
 446