【问题标题】:Getting 405 status Error when trying to consume POST method of web/Api in asp.net mvc5尝试在 asp.net mvc5 中使用 web/Api 的 POST 方法时出现 405 状态错误
【发布时间】:2020-02-10 07:05:29
【问题描述】:

我正在尝试使用以字符串为参数的 web/api 的 post 方法,但得到 405 错误:Api 控制器名称:Getdata 操作方法名称:postdata

请帮助找出错误。

public bool postdata(string fn)
        {
            using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://localhost:59968/api/Getdata");
                var task = client.PostAsJsonAsync("Getdata", fn);
                task.Wait();

                var res = task.Result;
                if(res.IsSuccessStatusCode)
                {
                    return true;
                }
            }
            return false;
        }

[HttpPost] public IHttpActionResult postdata(string p) { using (var context = new MediaEntities()) { mediatable m = new mediatable() { media_path = p, is_active = true }; context.mediatable.Add(m); context.SaveChanges(); return Ok(); } }

【问题讨论】:

    标签: asp.net-mvc-5


    【解决方案1】:

    我认为您使用了操作方法名称而不是控制器名称,否则请提供您的 api 的详细信息。

    client.BaseAddress = new Uri("http://localhost:59968/api/{controller_name}");
    
    //HTTP POST
    var postTask = client.PostAsJsonAsync("controller_name", fn);
    

    这个link 可能会对您有所帮助。

    【讨论】:

    猜你喜欢
    • 2017-11-03
    • 2019-01-30
    • 1970-01-01
    • 2022-01-27
    • 2019-03-20
    • 2014-12-04
    • 2010-10-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多