我正在寻找将参数从Ajax请求传递到ASP.Net核心中的Web的方法,就像经典query string中的query string一样。我在下面试过了,但没有起作用。
视图:
"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中实现这一目标的方法?
发布于 2017-02-23 06:31:24
"ajax":
{
"url": "/api/APIDirectory/GetDirectoryInfo"
"type": "POST",
"dataType": "JSON",
"data": {"reqPath":"@ViewBag.Title"}
}编辑:如果我们使用查询字符串,我们可以使用类型作为GET。
但是我们使用POST方法,所以我们需要用param " data“传递数据。
发布于 2017-02-23 06:27:06
在查询字符串中发布数据时,请使用内容类型application/x-www-form-urlencoded。
$.ajax({
type: "POST",
url: "/api/APIDirectory/GetDirectoryInfo?reqPath=" + @ViewBag.Title,
contentType: "application/x-www-form-urlencoded"
});另外,确保ajax语法是正确的(我在我的示例中使用了jQuery ),并且@ViewBag不包含在字符串中。
然后在控制器中添加FromUri参数,以确保绑定从uri读取。
[HttpPost]
public IActionResult GetDirectoryInfo([FromUri]string reqPath)
{
string requestPath = reqPath;
// some code here..
}https://stackoverflow.com/questions/42408150
复制相似问题