通过Put请求访问WebAPI中的方法

时间:2022-12-02 10:22:55

My apologies if the question seems trivial but being new to WebAPI i cannot get the client access Put Methods.
Here is my WebAPI Method

我很抱歉,如果这个问题看似微不足道,但对WebAPI来说是新手,我无法让客户端访问Put Methods。这是我的WebAPI方法

public class TestController : ApiController
{
    [HttpPut]
    public HttpResponseMessage Put(string id)
    {          
        var response = new HttpResponseMessage();
        response.Headers.Add("Message","Success");
        return response;
    }    
}

Route Configuration

 //configure the webapi so that it can self host 
 var config = new HttpConfiguration();
 config.MapHttpAttributeRoutes();
 config.Routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "{Controller}/{action}/{id}",
       defaults: new {id = RouteParameter.Optional }
 );

Client

var baseAddress="http://localhost:8000/";
var client= new HttpClient {BaseAddress = new Uri(baseAddress)};
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new    MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.PutAsJsonAsync(baseAddress+"Test", "TestParam").Result;
Console.WriteLine(response.Content);

When i run the above code. I get 404 Error for some reason meaning the client cannot contact the webapi method.

当我运行上面的代码。我因某种原因得到404错误,这意味着客户端无法联系webapi方法。

I have three questions.
1. How can i make sure the client can correctly call the web api method.
2. Is it possible to invoke the method directly from url? I tried http://localhost:8000/test/Dummy but no luck.
3. If its possible to invoke the method how can i make sure the message gets displayed on the page saying "Success".

我有三个问题。 1.如何确保客户端可以正确调用web api方法。 2.是否可以直接从url调用该方法?我试过http:// localhost:8000 / test / Dummy,但没有运气。 3.如果可以调用该方法,我如何确保消息在页面上显示“成功”。

1 个解决方案

#1


Your code would work if you didn't specify the action in the configured route. In that case the action is selected by the HTTP method (PUT, POST, DELETE, GET...). The route shpukd look like this:

如果未在配置的路由中指定操作,则代码将起作用。在这种情况下,动作由HTTP方法选择(PUT,POST,DELETE,GET ......)。路线shpukd看起来像这样:

routes.MapHttpRoute(
  name: "API Default",
  routeTemplate: "api/{controller}/{id}",
  defaults: new { id = RouteParameter.Optional }
);

As you've specified the {action} part in the route, you must specify the action in the url. In that case, the url should be like this:

由于您已在路径中指定了{action}部分,因此必须在URL中指定操作。在这种情况下,网址应该是这样的:

http://localhost:8000/Test/Put

Please, see Routing in ASP.NET Web API.

请参阅ASP.NET Web API中的路由。

If you want to test your Web API methods one of the best tools available is Postman, which is a Chrome extenion. This solves your questions 1 & 2.

如果您想测试Web API方法,可以使用的最好的工具之一是Postman,它是Chrome扩展程序。这解决了您的问题1和2。

For Q3: as you're making an asycn call, you have to await for the response message, read it, and show the corresponding message. You can read this documentation if you don't know the async/await model.

对于Q3:当您正在进行asycn调用时,您必须等待响应消息,读取它并显示相应的消息。如果您不知道async / await模型,可以阅读本文档。

#1


Your code would work if you didn't specify the action in the configured route. In that case the action is selected by the HTTP method (PUT, POST, DELETE, GET...). The route shpukd look like this:

如果未在配置的路由中指定操作,则代码将起作用。在这种情况下,动作由HTTP方法选择(PUT,POST,DELETE,GET ......)。路线shpukd看起来像这样:

routes.MapHttpRoute(
  name: "API Default",
  routeTemplate: "api/{controller}/{id}",
  defaults: new { id = RouteParameter.Optional }
);

As you've specified the {action} part in the route, you must specify the action in the url. In that case, the url should be like this:

由于您已在路径中指定了{action}部分,因此必须在URL中指定操作。在这种情况下,网址应该是这样的:

http://localhost:8000/Test/Put

Please, see Routing in ASP.NET Web API.

请参阅ASP.NET Web API中的路由。

If you want to test your Web API methods one of the best tools available is Postman, which is a Chrome extenion. This solves your questions 1 & 2.

如果您想测试Web API方法,可以使用的最好的工具之一是Postman,它是Chrome扩展程序。这解决了您的问题1和2。

For Q3: as you're making an asycn call, you have to await for the response message, read it, and show the corresponding message. You can read this documentation if you don't know the async/await model.

对于Q3:当您正在进行asycn调用时,您必须等待响应消息,读取它并显示相应的消息。如果您不知道async / await模型,可以阅读本文档。