如果搜索词中包含特殊字符,则
我是mvc的新手,我想发送一个带有参数的get请求,但是服务器端的参数总是空的。在客户端,我在js文件中使用angular执行以下请求:
$http.get('api/person/getPersons', "TEST");
我的PersonController有以下方法:
/// <summary>
/// Get all persons from the database.
/// </summary>
/// <returns>All persons.</returns>
[HttpGet]
[ActionName("getPersons")]
public IHttpActionResult GetPersons(string personToFind)
{
return this.Content(HttpStatusCode.OK, personToFind);
}
在这种情况下,字符串保持为空,也尝试了以下两个选项,但它们都不起作用。
/// <summary>
/// Get all persons from the database.
/// </summary>
/// <returns>All persons.</returns>
[HttpGet]
[ActionName("getPersons")]
public IHttpActionResult GetPersons([FromBody] string personToFind)
{
return this.Content(HttpStatusCode.OK, personToFind);
}
.
/// <summary>
/// Get all persons from the database.
/// </summary>
/// <returns>All persons.</returns>
[HttpGet]
[ActionName("getPersons")]
public IHttpActionResult GetPersons([FromUri] string personToFind)
{
return this.Content(HttpStatusCode.OK, personToFind);
}
我也试图从'personToFind'创建一个对象,但这给了我一个空的System.object。
如果有人知道我做错了什么,我会很高兴。
尝试$http.get('api/person/getPersons?personToFind=TEST")
与[FromUri]
的类型。
$http.get('api/person/getPersons?personToFind=' + encodeURIComponent("TEST"))
将更安全。