POST字符串的ASP.NET Web API应用 - 返回null [英] POST string to ASP.NET Web Api application - returns null

查看:129
本文介绍了POST字符串的ASP.NET Web API应用 - 返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着去传递从客户机到ASP.NET应用程序MVC4一个字符串。

Im trying to transmit a string from client to ASP.NET MVC4 application.

但我不能接受字符串,要么是空或POST方法无法找到(404错误)

But I can not receive the string, either it is null or the post method can not be found (404 error)

客户端code传输字符串(控制台应用程序):

Client Code to transmit the string (Console Application):

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:49032/api/test");
request.Credentials = new NetworkCredential("user", "pw");
request.Method = "POST";
string postData = "Short test...";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;

Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();

WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();

StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
Console.ReadLine();

的ASP.NET Web API控制器:

ASP.NET Web Api Controller:

public class TestController : ApiController
{
    [Authorize]
    public String Post(byte[] value)
    {
        return value.Length.ToString();
    }
}

在这种情况下,我能称之为邮报的方法,但价值是 NULL
如果我改变方法签名(字符串值),比它永远不会被调用。

In that case I'm able to call the "Post" method, but "value" is NULL. If I change the method signature to (string value) than it will never called.

即使是没有的[授权]设定它具有相同的奇怪的行为。 - >因此无关与用户认证

Even "without" the [Authorize] setting it has the same strange behavior. -> So it has nothing to do with the user authentication.

任何想法,我做错了什么?
我感谢任何帮助。

Any ideas what I'm doing wrong? I'm grateful for any help.

推荐答案

您似乎已经使用了一些 [授权] 属性上的Web API控制器操作,我不看不到这是怎么有关你的问题。

You seem to have used some [Authorize] attribute on your Web API controller action and I don't see how this is relevant to your question.

那么,让我们付诸实践。下面是一个简单的Web API控制器如何可能是这样的:

So, let's get into practice. Here's a how a trivial Web API controller might look like:

public class TestController : ApiController
{
    public string Post([FromBody] string value)
    {
        return value;
    }
}

和为此事消费者:

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            var data = "=Short test...";
            var result = client.UploadString("http://localhost:52996/api/test", "POST", data);
            Console.WriteLine(result);
        }
    }
}

您无疑会注意到<一href=\"http://msdn.microsoft.com/en-us/library/system.web.http.frombodyattribute%28v=vs.108%29.aspx\"><$c$c>[FromBody]在Web API控制器属性的装饰以及POST数据的 = preFIX奥姆客户端。我建议你​​阅读<一href=\"http://blogs.msdn.com/b/jmstall/archive/2012/04/16/how-webapi-does-parameter-binding.aspx\">how并在Web API并参数绑定的以更好地理解概念。

You will undoubtedly notice the [FromBody] decoration of the Web API controller attribute as well as the = prefix of the POST data om the client side. I would recommend you reading about how does the Web API does parameter binding to better understand the concepts.

至于 [授权] 属性而言,这可以用来保护你的服务器上的一些动作从暂时只有通过身份验证的用户访问。其实这是pretty不清楚你想要达到here.You应当在路上你的问题作出这更清楚。你正试图了解参数绑定中的ASP.NET Web API是如何工作的(请阅读文章我已经联系到,如果这是你的目标),或试图做一些认证和/或授权?如果第二个是你的情况下,你可能会发现下面的帖子的 了我对这个话题感兴趣的让你开始写了。

As far as the [Authorize] attribute is concerned, this could be used to protect some actions on your server from being accessible only to authenticated users. Actually it is pretty unclear what you are trying to achieve here.You should have made this more clear in your question by the way. Are you are trying to understand how parameter bind works in ASP.NET Web API (please read the article I've linked to if this is your goal) or are attempting to do some authentication and/or authorization? If the second is your case you might find the following post that I wrote on this topic interesting to get you started.

如果看完我已经联系到资料后,你和我一样,对自己说,跆拳道的人,我需要做的是POST字符串到服务器端的端点,我需要做的这一切?没门。然后结帐 ServiceStack 。你将不得不与网页API比较了良好的基础。我不知道在微软的帅哥都想着设计的Web API时,不过来了,说真的,我们应该有我们的HTML独立的基控制器(想想剃刀)和REST的东西?这不可能是认真的。

And if after reading the materials I've linked to, you are like me and say to yourself, WTF man, all I need to do is POST a string to a server side endpoint and I need to do all of this? No way. Then checkout ServiceStack. You will have a good base for comparison with Web API. I don't know what the dudes at Microsoft were thinking about when designing the Web API, but come on, seriously, we should have separate base controllers for our HTML (think Razor) and REST stuff? This cannot be serious.

这篇关于POST字符串的ASP.NET Web API应用 - 返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆