如何将参数从Ajax请求传递到Web API控制器? [英] How to pass parameter from Ajax Request to Web API Controller?

查看:159
本文介绍了如何将参数从Ajax请求传递到Web API控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找将参数从Ajax Request传递到ASP.Net Core中的Web API Controller的方法,就像经典ASP中的query string一样. 我在下面尝试过,但是没有用.

I am looking for the way to pass the parameter from Ajax Request to Web API Controller in ASP.Net Core like query string in classic ASP. I have tried below but it did not worked.

查看:

"ajax":
    {
        "url": "/api/APIDirectory/GetDirectoryInfo?reqPath=@ViewBag.Title"
        "type": "POST",
        "dataType": "JSON"
    },

控制器:

[HttpPost]
public IActionResult GetDirectoryInfo(string reqPath)
{   
    string requestPath = reqPath;
    // some code here..
}

任何人都可以在asp.net核心网络api中提出实现此目标的方法吗?

Can anyone please advise the ways available to achieve this in asp.net core web api?

推荐答案

"ajax":
{
    "url": "/api/APIDirectory/GetDirectoryInfo"
    "type": "POST",
    "dataType": "JSON",
    "data": {"reqPath":"@ViewBag.Title"}
}

如果使用查询字符串,则可以将类型用作GET.

Edited: If we use query string, we can use the type as GET.

但是我们使用的是POST方法,因此我们需要使用参数"data"传递数据.

But we are using POST method, so we need to pass the data with the param "data".

这篇关于如何将参数从Ajax请求传递到Web API控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆