🙂 İNSANLARIN EN HAYIRLISI INSANLARA FAYDALI OLANDIR 🙂

Ramazan HABER / C# MVC / Action Results 'lar Json,File,RedirectToAction,View,PartialView,EmptyResult vb...

1-) C# MVC - Action Results 'lar Json,File,RedirectToAction,View,PartialView,EmptyResult vb...

 

 

1-) C# MVC - RedirectToAction -> başka controllerdeki action'u çağırır. Kısaca Action çağırma

 çağırma işlemi ;

public class DepartmanController : Controller

    { public ActionResult test()

        {

            Departman departman = new Departman { Id = 1, Ad = "Bilgi İşlem" };

            return RedirectToAction("Index","Personel");// Index = ActionName , Personel = ControllerName

        }}

Çağrılan Yer ;

public class PersonelController : Controller

    {

        // GET: Personel

        public ActionResult Index()

        {

            return Content("Dışarıdan Çağrıldı");

        }

    }

 

2-) ActionResulta Parametre göndermek

/*

         http://localhost:50003/departman//Sil/5 veya http://localhost:50003/departman//Sil?id=99 -> App_Start -> RouteConfig.cs -> id olduğu için

         ama soru işareti(?) ile gönderirsen parametre adını değiştirebilirsin o yüzden hep soru işaretini kullan bence

             */

        public ActionResult Sil(int id) // yazdığı için parametre adı id oldu

        {

            return Content("Gelen ID : "+id);

        }

 

//Soru işareti ile çağırırsan aşağıdaki gibi parametre adını değiştirebilirsin

 

public ActionResult Sil(int Personelid) // http://localhost:50003/departman//Sil?Personelid=99

        {

            return Content("Gelen ID : " + Personelid);

        }

 

//Birden Fazla parametre gönderirsen araya & işareti koyarsın

 

public ActionResult Sil(int Personelid,string ad)//http://localhost:50003/departman//Sil?Personelid=99&ad=ramazan

        {

            return Content("Gelen ID : " + Personelid+" Ad: "+ad);

        }

 

3-) Route yani URL 'i kendin tanımlama -> App_Start->RouteConfig.cs -> aşağıdakini ekle

 

routes.MapMvcAttributeRoutes(); -> ekle -> hepsi aşağıdaki gibi olsun ->

 

using System.Web.Mvc;

using System.Web.Routing;

namespace PersonelMVC

{

    public class RouteConfig

    {

        public static void RegisterRoutes(RouteCollection routes)

        {

            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapMvcAttributeRoutes();

            routes.MapRoute(

                name: "Default",

                url: "{controller}/{action}/{id}",

                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

            );     }    }}

 

YUKARIDAKİNİ YAPTIKTAN SONRA AŞAĞIDAKİNİ ROUTE KULLANARAK YAPABİLİRSİN

/*

       Route kullanırsan =  http://localhost:50003/personel/liste/1/ramazan

       Route kullanmadan =  http://localhost:50003/departman//Sil?Personelid=99&ad=ramazan

NOT: Hep Route Kullan çünkü metot ismi değişirse bile Route değişmez

         */

        [Route("personel/liste/{Personelid}/{ad}")]

        public ActionResult Sil(int Personelid,string ad) //

        {

            return Content("Gelen ID : " + Personelid+" Ad: "+ad);

        }

 

 

4-) JSON

 

  public ActionResult Test()

        {

            Departman dep = new Departman() { Id = 1, Ad = "Bilgi İşlem" };

 

            return Json(dep,JsonRequestBehavior.AllowGet);

        }

 2021 Ocak 18 Pazartesi
 403