JsonResult post ile erişim

JsonResult ile POST isteği yapmak (özellikle ASP.NET MVC veya ASP.NET Core ortamında), genellikle jQuery ya da fetch kullanılarak yapılır.

Aşağıda ASP.NET MVC veya ASP.NET Core için bir JsonResult endpoint’ine POST ile nasıl erişileceğini gösteren örnekleri bulabilirsin:

 

1. Sunucu Tarafı (Controller) – JsonResult (C#)

[HttpPost]
public JsonResult SaveData(MyModel model)
{
    if (ModelState.IsValid)
    {
        // Veritabanına kayıt veya işlem
        return Json(new { success = true, message = "Kayıt başarılı" });
    }

    return Json(new { success = false, message = "Geçersiz veri" });
}

 

2. İstemci Tarafı (JavaScript ile erişim)

jQuery ile POST isteği

$.ajax({
    type: "POST",
    url: "/Home/SaveData", // Controller/Action
    data: JSON.stringify({ name: "Ahmet", age: 30 }), // Gönderilen model
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (response) {
        if (response.success) {
            alert(response.message);
        } else {
            alert("Hata: " + response.message);
        }
    }
});

 

Fetch API ile POST (modern JS)

fetch("/Home/SaveData", {
    method: "POST",
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({ name: "Ahmet", age: 30 })
})
.then(response => response.json())
.then(data => {
    if (data.success) {
        console.log("Başarılı:", data.message);
    } else {
        console.error("Hata:", data.message);
    }
});