简体   繁体   English

ASP.NET WebApi Post方法 - 404传递参数时

[英]ASP.NET WebApi Post Method - 404 When Passing Parameters

I cannot for the life of me figure this out. 我不能为我的生活弄清楚这一点。 I have a web api controller with Get and Post methods. 我有一个带有Get和Post方法的web api控制器。 The Get method works fine with and without parameters, but the post breaks when I try to add a String parameter to it. 使用和不使用参数时,Get方法可以正常工作,但是当我尝试向其添加String参数时,帖子会中断。 Below is my code. 以下是我的代码。

Route: 路线:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "{controller}/{id}",
            defaults: new { id = UrlParameter.Optional }
        );

Controller: 控制器:

public class AuditController : ApiController
{
    public String Post(String test)
    {
        return "Success : " + test;
    }

    public String Get(String test)
    {
        return "Success : " + test;
    }
}

Request: 请求:

    var request = WebRequest.Create("http://localhost:42652/Audit");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        using (var writer = new StreamWriter(request.GetRequestStream()))
        {
            writer.Write("test=TEST");
        }
        WebResponse webResponse = request.GetResponse();

I've tried many variations on the request, I feel like there's something simple I'm missing. 我已经在请求上尝试了很多变化,我觉得我有一些简单的缺失。 Thanks for your help. 谢谢你的帮助。

Since you are expecting parameter test to come from body of a request, you would need to decorate it with FromBody attribute. 由于您希望参数test来自请求的主体,因此您需要使用FromBody属性来装饰它。 Example: ([FromBody]String test) . 示例: ([FromBody]String test) This is not true for other complex types, for example: Employee class which is implicitly considered to be coming from Body. 对于其他复杂类型,情况并非如此:例如:隐式地认为来自Body的Employee类。

Rearding GET request. 等待GET请求。 It should be working only with test coming from query string /Audit?test=Mike 它应该只适用于来自查询字符串/Audit?test=Mike

Following blog post has more details about parameter binding: http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx 以下博客文章提供了有关参数绑定的更多详细信息: http//blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx

Also, I see that you are using WebRequest. 另外,我看到你正在使用WebRequest。 Have you considered using HttpClient from System.Net.Http instead? 您是否考虑过使用System.Net.Http中的HttpClient?

Change your AuditController to include the FromBody attribute : 更改AuditController以包含FromBody属性:

public class AuditController : ApiController
{
    public String Post([FromBody]String test)
    {
        return "Success : " + test;
    }

    public String Get(String test)
    {
        return "Success : " + test;
    }
}

cf. 比照 http://msdn.microsoft.com/en-us/library/system.web.http.frombodyattribute(v=vs.108).aspx http://msdn.microsoft.com/en-us/library/system.web.http.frombodyattribute(v=vs.108).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM