使用http向服务器控制器发送一个字符串.get - ASP.净MVC



我是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"))将更安全。

最新更新