【问题标题】:Post data through Url to webapi通过 Url 将数据发布到 webapi
【发布时间】:2017-01-23 12:18:36
【问题描述】:

嘿嘿,

我无法通过查询字符串将数据发布到操作方法,该操作方法位于下面的控制器类中是我的代码。

我输入一个网址“http://localhost:53459/api/esb/post/test”来发布价值,但没有任何反应

任何帮助将不胜感激。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{value}",
            defaults: new { value = RouteParameter.Optional }
        );
    }
}



[RoutePrefix("api/esb")]
public class EsbController : ApiController
{
    [Route("get")]
    [HttpGet]
    public string Get()
    {
        return "Hello there!";
    }

    [Route("post")]
    [HttpPost]
    [AcceptVerbs("POST")]
    public string Post([FromUri]string value)
    {
        return string.Format("{0} is posted successfully ", value);
    }

    [Route("put")]
    [HttpPut]
    public string Put([FromUri] string value)
    {
        return string.Format("{0} is updated successfully ", value);
    }

    [Route("delete")]
    [HttpDelete]
    public string Delete(string value)
    {
        return string.Format("{0} is deleted successfully ", value);
    }
}

【问题讨论】:

  • 您不需要在 url 中使用 POST。所以应该是这样的:http://localhost:53459/api/esb/test

标签: asp.net-web-api


【解决方案1】:

如果您在浏览器中输入 url,您正在构建一个 GET 请求,因此它永远不会到达您的 Post 操作。您可以通过将“GET”添加到操作上允许的动词来确认这一点(注意:删除 [HttpPost] 属性)。

[Route("post")]
[AcceptVerbs("POST", "GET")]
public string Post([FromUri]string value)
{
  return string.Format("{0} is posted successfully ", value);
}

【讨论】:

  • 为什么要使用get?。看起来方法的行为类似于 get
  • 这只是为了说明为什么你的操作没有被击中。如其他地方所述,除非您使用 Postman 或类似工具,否则您无法通过浏览器发出 Post 请求。
【解决方案2】:

移除参数绑定 [FromUri] 并像更新路由一样

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{value}",
            defaults: new { value = RouteParameter.Optional }
        );
    }
}

控制器

[RoutePrefix("api/esb")]
public class EsbController : ApiController
{
    [Route("get")]
    [HttpGet]
    public string Get()
    {
        return "Hello there!";
    }

    [Route("post/{value}")]
    [HttpPost]
    [AcceptVerbs("POST")]
    public string Post(string value)
    {
        return string.Format("{0} is posted successfully ", value);
    }

}

这对我有用,尝试在 chrome 或 fiddler 中使用 Postman。

POST http://localhost:XXXXX/api/esb/post/test HTTP/1.1

【讨论】:

  • 感谢您的回复。我试过这个。这没用。你知道它有什么问题吗
  • 如果我在邮递员中尝试这个。它工作正常,但如果我在浏览器中尝试。它没有显示任何内容
  • 您不能使用浏览器进行发布请求。如果您想在网站中使用创建 ajax 请求。检查这篇文章。 stackoverflow.com/questions/4797534/…
【解决方案3】:

对于 dot.Net Core(我使用的是 v2.0),使用 FromRoute

[AllowAnonymous]
[HttpPost]
[Route("validateEmail/{validationCode}")]
public async Task<IActionResult> ValidateEmail(
 [FromRoute] Guid validationCode)
    {
        await authService.ValidateEmailAsync(validationCode);

        return Ok();
    }

然后像这样发帖:

【讨论】:

    猜你喜欢
    • 2023-01-01
    • 1970-01-01
    • 2018-11-22
    • 2023-02-17
    • 2012-04-30
    • 2018-07-12
    • 1970-01-01
    • 2013-10-17
    • 2019-01-30
    相关资源
    最近更新 更多