How to make PUT from ASP.NET MVC to ASP.NET Core Web API?

itsArber

I am having trouble with the put method of Web API. I am working with Kendo UI Jquery inside ASP.NET MVC and it has to make a PUT through API.

Please guide me what am I doing wrong, also at the end of this is the error code.

Here it is what I have tried so far:

API controller:

[HttpPut] //It never reaches here
[Route("api/Update")]
public async Task<IActionResult> Update(Guid id, UpdateProductCommand command)
{
    if (id != command.Id)
    {
        return BadRequest();
    }

    return Ok(await _mediator.Send(command));
}

ASP.NET MVC controller:

public ActionResult Update(Guid id, [FromBody]Product product)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://localhost:44393");
            
        var json = JsonConvert.SerializeObject(product);

        var contentData = new StringContent(json, Encoding.UTF8, "application/json");

        var response = client.PutAsync("/api/Product", contentData);

        var result = response.Result;

        if (result.IsSuccessStatusCode)
        {
            return RedirectToAction("Index");
        }
    }

    return View(product);
}

View with Kendo UI:

<script>
    //----------TRANSPORT -------------------//
    var dataSource = new kendo.data.DataSource({
        batch: false,
        transport: {
            read: {
                url: "https://localhost:44393/api/Product",
                dataType: "json"
            },
            update: {
                url: "@Url.Action("Update", "Home")",
                dataType: "json",
            },
            create: {
                url: "@Url.Action("Create", "Home")",
                dataType: "json",
                contentType: "application/json",
                type: "POST"
            },
            destroy: {
                url: "@Url.Action("Delete", "Home")",
                dataType: "json",

            },
           },
        pageSize: 5,
        schema: {
            model: {
                id: "id",
                fields: {
                    id: { editable: false, nullable: true },
                    productName: { type: "string", editable: true },
                    prouctSKU: { type: "string", editable: true },
                    productType: { type: "string", editable: true },
                }
            }
        }
    });

Error:

jquery.min.js:4 GET https://localhost:44385/Home/Update?id=1e8332f1-ae69-4997-b851-08d9ae81e7de&productName=sd&prouctSKU=string&productType=string 415

Max

In your MVC Controller you are using PutAsync in a non async method. Change your Action to an async method, change the ActionResult to async Task<ActionResult> and use the await keyword when calling the PutAsync.

Or if you do not want to change your method to an async method change the call client.PutAsync to client.Put

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to call ASP.NET Core Web MVC from Web API

Posting a file from ASP.NET MVC 4.6 application to ASP.NET Core Web API

How to seperate ASP.NET MVC core view and ASP.NET WEB API controllers into separate projects?

How to make HTTP call from Controller ? to Use web API's Asp.Net Core C#

How get data from asp.net web api in asp.net mvc controller in the same project

ASP.NET Core Web API - PUT vs PATCH

How to return XML from ASP.NET 4 Web API to ASP.NET Core 2 application?

ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

Post files from ASP.NET Core web api to another ASP.NET Core web api

ASP.NET Core MVC & ASP.NET Core Web API in same project

ASP.NET Core MVC and 2 Web API projects

async-await in asp.net core web api (not MVC)

Inside my asp.net core MVC web application how i can make the recaptcha a required field

Download file from dotnet Core Web API using POST in ASP.NET MVC 5 controller

Consuming Web API secured with JWT authentication from ASP.NET Core MVC application

How to make ASP.NET Core MVC routes generation relative?

How to make Cascading dropdown in asp.net core mvc?

How to make a dashboard with ASP.NET Core MVC

How to make an Authorized HttpWebRequest to an ASP.NET MVC web app

How to add Web API controller to an existing ASP.NET Core MVC?

How to avoid the circular referencing asp.net core mvc [HttpPost("add-recipe")] web api

How to make ASP.NET Core Web API action execute asynchronously?

How to make autofac KeyFilter works in asp.net-core web api?

How to make a new controller default controller in asp.net core web api

How to make ASP.Net Core Web API Identity return a 401 on unauthorized

How to make email case insensitive authentication in ASP.NET Core Web API login

Running ASP.NET MVC 6 Web API from IIS

Access Web API routing from ASP.NET MVC application

Call asp.net core web api from PostMan