简体   繁体   中英

Access to web API from a console application

In the process of learning about web API, I have created (or actually Visual Studio has) a simple controller. I have also created a WPF program that can read from web API's GET() part, but I can't access the others.

I have tried a lot and found many blogs and pages that say: "Just do like this..." but nothing works. What am I doing wrong?

MVC part:

namespace MVCWebservice.Controllers
{
    public class LaLaController : ApiController
    {
        // GET: api/LaLa
        public string Get()
        {
            return "Hello from API";
        }

        // GET: api/LaLa/5
        public string Get(int id)
        {
            return "value";
        }

        // POST: api/LaLa
        public void Post([FromBody]string value)
        {
            var a = value;
        }

        // PUT: api/LaLa/5
        public void Put(int id, [FromBody]string value)
        {
            var b = value;
            int c = id;
        }

        // DELETE: api/LaLa/5
        public void Delete(int id)
        {
            int c = id;
        }
    }
}

And a method from my Console application that actually works:

private static async Task ReadFromWebApi()
{
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    client.BaseAddress = new Uri("http://localhost:26176/");
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    var resp2 = await client.GetAsync("api/LaLa/");
    resp2.EnsureSuccessStatusCode();
    var aaa = resp2.Content;
    string result = await aaa.ReadAsStringAsync();
    Console.WriteLine(result);
}

A method that just stops:

If I remove the EnsureSuccessStatusCode I'll get the following back:

ss = "{\\"Message\\":\\"The requested resource does not support http method 'PUT'.\\"}"

private static async Task SendToWebApi()
{
    System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
    client.BaseAddress = new Uri("http://localhost:26176/");
    client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
    Console.WriteLine("-1-");
    var resp2 = client.PutAsync("api/LaLa/", new System.Net.Http.StringContent("Hey", Encoding.UTF8, "application/json")).Result;
    Console.WriteLine("-2-");
    resp2.EnsureSuccessStatusCode();
    var ss = await resp2.Content.ReadAsStringAsync();
}

How would I write my access to the other methods?

For one thing, you are calling PUT on the URL api/LaLa/ , but from the server's method signature

public void Put(int id, [FromBody]string value)

it appears that the URL should include a numeric id to satisfy the first parameter, eg api/LaLa/100 or similar. Perhaps you could modify your client to call PutAsync() like this:

var resp2 = client.PutAsync("api/LaLa/100", new System.Net.Http.StringContent("Hey", Encoding.UTF8, "application/json")).Result;

Does POST work? Look at this answer to a question that looks remarkably like yours, and see if you can get that to work.

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