简体   繁体   中英

Asp.net Client - Server POST and Response (405 Method Not Allowed)

Why doesn't my server return the same value to the client as he sends?


On the server my ApiController looks like that:

public void Post(byte[] value)
    {
        Request.CreateResponse<byte[]>(HttpStatusCode.OK, value);
    }

- Shouldn't it return the data from POST to the client?
And on the client side I've got:

using (var client = new WebClient())
{
    byte[] value = { 16, 32 };

    client.UploadDataCompleted += delegate
    {
        Debug.WriteLine(Encoding.Default.GetString(response));
    };

    response = client.UploadData("http://localhost:52117/", value);
}

The error message:

The remote server returned an error: (405) Method Not Allowed.

I just started with ASP.net. I am trying to make it work for hours now lol.
I've tried to follow tutorial with products and stuff, but it's not even similar.

Edit: I even tried this thing Here where it says:

The return value from the action is converted to an HttpResponseMessage.

If return type is HttpResponseMessage, pass through (as response to client) .

public HttpResponseMessage Post(byte[] value)
{
    var msg = new HttpResponseMessage();
    msg.Content = new ByteArrayContent(value);
    return msg;
}

Nothing works..

you might missing something

Refer to this link for more idea, https://trentgardner.net/net/asp-net-webmethods-with-jquery-and-ajax/

try

 [System.Web.Services.WebMethod]
public void Post(byte[] value)
    {
        Request.CreateResponse<byte[]>(HttpStatusCode.OK, value);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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