【问题标题】:How to make PUT from ASP.NET MVC to ASP.NET Core Web API?如何从 ASP.NET MVC 到 ASP.NET Core Web API 的 PUT?
【发布时间】:2022-01-02 16:29:22
【问题描述】:

我在使用 Web API 的 put 方法时遇到问题。我正在 ASP.NET MVC 中使用 Kendo UI Jquery,它必须通过 API 进行 PUT。

请指导我做错了什么,这也是错误代码。

这是我迄今为止尝试过的:

API 控制器:

[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 控制器:

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);
}

使用 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 },
                }
            }
        }
    });

错误:

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

【问题讨论】:

    标签: c# asp.net .net asp.net-mvc asp.net-core


    【解决方案1】:

    在您的 MVC 控制器中,您在非 async 方法中使用 PutAsync。 将您的操作更改为async 方法,将ActionResult 更改为async Task&lt;ActionResult&gt; 并在调用PutAsync 时使用await 关键字。

    或者,如果您不想将方法更改为 async 方法,请将调用 client.PutAsync 更改为 client.Put

    【讨论】:

    【解决方案2】:

    您需要告诉您的组件发出PUT 请求,否则它将默认为GET。根据docs,你应该指定请求的type。例如:

    update: {
        url: "@Url.Action("Update", "Home")",
        dataType: "json",
        type: "PUT" //<---- Add this
    },
    

    【讨论】:

    • 谢谢大卫的回答,已经试过了但是不行,我想我的问题可能出在我的 MVC 控制器上。
    猜你喜欢
    • 2021-10-12
    • 1970-01-01
    • 2017-07-22
    • 2021-12-08
    • 1970-01-01
    • 2017-11-01
    • 2017-01-16
    • 1970-01-01
    • 2014-03-15
    相关资源
    最近更新 更多