httproutesasp.net-core-mvcasp.net-core-webapi

ASP.NET Core MVC


I have this issue on my blog app project where I want to update or delete an author from my list and with that send http clients from MVC to API but It doesn't work. I tried it from API and It works normally. The problem is sending clients to api from mvc. I can't update or delete from mvc but I can from Api. I can create a new author from mvc so Im thinking the problem is routing. But I cant solve it. Im gonna post my codes and maybe u guys can review it and tell me what the problem is maybe?

[HttpPost]
public async Task<IActionResult> Update(UpdateAuthorDTO author)
{
    if (!ModelState.IsValid)
    {
        var errors = ModelState.Values.SelectMany(v => v.Errors);
        foreach (var error in errors)
        {
            Console.WriteLine(error.ErrorMessage); // Hata mesajlarını konsola yazdır.
        }
        return BadRequest(ModelState); // Model hatalarını döner.
    }


    // Güncelleme işlemi için API çağrısı yapalım
    using (var httpClient = new HttpClient())
    {
        var form = new MultipartFormDataContent();
        form.Add(new StringContent(author.Id.ToString()), "Id");
        form.Add(new StringContent(author.FirstName), "FirstName");
        form.Add(new StringContent(author.LastName), "LastName");

        // Eğer bir resim yüklenmişse, dosyayı da ekle
        if (author.Image != null)
        {
            var fileStreamContent = new StreamContent(author.Image.OpenReadStream());
            form.Add(fileStreamContent, "UploadPath", author.Image.FileName);
        }
        //todo: buradak 404 not found olayını çöz
        //string uri = "https://localhost:7296";
        var response = await httpClient.PostAsync($"{uri}/api/Author/Update", form);
        if (response.IsSuccessStatusCode)
        {
            TempData["Success"] = $"{author.FirstName} {author.LastName} kişisinin kaydı başarıyla güncellendi!";
        }
        else
        {
            TempData["Error"] = "Yazar kaydı güncellenirken bir hata meydana geldi.";
            return View(author);
        }
    }

    return RedirectToAction(nameof(Index)); // Güncelleme başarılıysa Index sayfasına yönlendir.
}

[HttpPost]
[ValidateAntiForgeryToken] // CSRF koruması için
public async Task<IActionResult> Delete(int id) //TODO Silme işlemini buradan hallet.
{
    using (var httpClient = new HttpClient())
    {
        var response = await httpClient.DeleteAsync($"{uri}/api/Author/Delete/{id}");
        if (response.IsSuccessStatusCode)
        {
            TempData["Success"] = "Yazar başarıyla silindi!";
        }
        else
        {
            TempData["Error"] = "Yazar silinirken bir hata meydana geldi.";
            return RedirectToAction(nameof(Index));
        }
    }

    return RedirectToAction(nameof(Index)); // İşlem tamamlandığında liste sayfasına yönlendir
}

Im trying to update and delete from MVC UI but when I try to update I get an 404 not found error at the code "var response = await httpClient.PostAsync($"{uri}/api/Author/Update", form);" if this code succeeds, I get to see the index page where the updated author also is.

When I want to delete I get an 405 This page could not be found error.

You can also see my AuthorController api file down below:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OnionProject.Application.Models.DTOs;
using OnionProject.Application.Services.AbstractServices;

namespace OnionProject.API.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthorController : ControllerBase
    {
        private readonly IAuthorService _authorService;

        public AuthorController(IAuthorService authorService)
        {
            _authorService = authorService;
        }

        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var authorList = await _authorService.GetAuthors();
            return Ok(authorList); // Burada AuthorVm kullanılıyor.
        }

        [HttpGet("{id}")]
        public async Task<IActionResult> Details(int id)
        {
            var authorDetail = await _authorService.GetDetail(id);
            if (authorDetail == null)
            {
                return NotFound();
            }
            return Ok(authorDetail);
        }


        [HttpPost]
        public async Task<IActionResult> Create([FromForm] CreateAuthorDTO author)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            bool exists = await _authorService.IsAuthorExists(author.FirstName, author.LastName);
            if (exists)
            {
                return Conflict("Bu isimde bir yazar zaten mevcut.");
            }

            // Resim dosyasının varlığını kontrol et
            if (author.Image != null)
            {
                // Dosya adını oluştur
                var fileName = Guid.NewGuid().ToString() + Path.GetExtension(author.Image.FileName);

                // Dosya kaydetme yolu
                var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/images", fileName);

                // Dosyayı kaydet
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    await author.Image.CopyToAsync(stream);
                }

                // Veritabanına kaydedilecek resim yolu
                author.ImagePath = "/images/" + fileName;
            }

            await _authorService.Create(author);
            return Ok();
        }

        [HttpPost("Update")]
        public async Task<IActionResult> Update([FromForm] UpdateAuthorDTO model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }


            // Güncelleme işlemi sadece yapılacak, sonuç kontrol edilmeyecek.
            await _authorService.Update(model);

            return Ok(); // Başarıyla sonuçlanmış kabul edilecek.
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            // Yazarın var olup olmadığını kontrol et
            var author = await _authorService.GetById(id);
            if (author == null)
            {
                return NotFound(new { message = "Yazar bulunamadı" });
            }

            // Yazar silme işlemini gerçekleştir
            await _authorService.Delete(id);

            // Başarılı olduğunda 204 No Content dön
            return NoContent(); // Silme başarılı, 204 No Content döner
        }



    }
}


Solution

  • I create a sample in my side. Here's my method in my MVC controller.

    [HttpGet]
    public async Task<IActionResult> callApi()
    {
        using (var httpClient = new HttpClient())
        {
            var form = new MultipartFormDataContent();
            form.Add(new StringContent("testUser"), "UserName");
            form.Add(new StringContent("password1"), "Password");
            var response = await httpClient.PostAsync($"https://localhost:7168/api/WeatherForecast/Update/Update", form);
            if (response.IsSuccessStatusCode)
            {
                var res = response.Content.ReadAsStringAsync();
            }
        }
        return Ok();
    }
    
    [HttpGet]
    public async Task<IActionResult> Delete(int id)
    {
        using (var httpClient = new HttpClient())
        {
            var response = await httpClient.DeleteAsync($"https://localhost:7168/api/WeatherForecast/Delete/{id}");
            if (response.IsSuccessStatusCode)
            {
                var res = response.Content.ReadAsStringAsync();
            }
        }
        return Ok();
    }
    

    Here's my API controller and the model I used:

    [ApiController]
    [Route("api/[controller]/[action]")]
    public class WeatherForecastController : ControllerBase
    {
        [HttpPost("Update")]
        public async Task<IActionResult> Update([FromForm] LoginModel model)
        {
            return Ok("success");
        }
    
        [HttpDelete("{id}")]
        public async Task<IActionResult> Delete(int id)
        {
            return NoContent();
        }
    }
    
    public class LoginModel
    {
        public string? UserName { get; set; }
        public string? Password { get; set; }
    }
    

    Just what I entered in the comment, we have [Route("api/[controller]/[action]")] on the api controller and [HttpPost("Update")] on the action method public async Task<IActionResult> Update, it will make the endpoint to be /api/controller/Update(action method name)/Update(defined in [HttpPost("Update")]).

    Test result: enter image description here enter image description here