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

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

问题描述

我正在尝试将字符串从客户端传输到 ASP.NET MVC4 应用程序.

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

但是我收不到字符串,要么是null,要么就是找不到post方法(404错误)

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

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

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();
    }
}

在那种情况下,我可以调用Post"方法,但value"是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.

即使没有"[Authorize] 设置,它也有同样奇怪的行为.-> 所以它与用户认证无关.

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 控制器操作上使用了一些 [Authorize] 属性,但我看不出这有什么相关性回答你的问题.

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);
        }
    }
}

您无疑会注意到 [FromBody] 修饰 Web API 控制器属性以及来自客户端的 POST 数据的 = 前缀.我建议您阅读有关 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.

[Authorize] 属性而言,这可用于保护您的服务器上的某些操作仅可由经过身份验证的用户访问.实际上,您在这里尝试实现的目标还不清楚.顺便说一下,您应该在问题中更清楚地说明这一点.您是想了解参数绑定在 ASP.NET Web API 中是如何工作的(如果这是您的目标,请阅读我链接到的文章)还是正在尝试进行一些身份验证和/或授权?如果您是第二种情况,您可能会发现我在这个主题上写的 following post 很有趣让你开始.

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.

如果在阅读了我链接到的材料后,您像我一样对自己说,WTF 伙计,我需要做的就是将一个字符串 POST 到服务器端端点,而我需要做所有这些?没门.然后结帐 ServiceStack.您将拥有与 Web API 进行比较的良好基础.我不知道微软的那些家伙在设计 Web API 时是怎么想的,但是说真的,我们应该为我们的 HTML(想想 Razor)和 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天全站免登陆