简体   繁体   English

WebApi-请求的资源不支持http方法“ GET”

[英]WebApi - The requested resource does not support http method 'GET'

Question Background: 问题背景:

I have a basic WebApi project hosted as a WebApp in Azure. 我有一个基本的WebApi项目作为Azure中的WebApp托管。

The Issue: 问题:

The problem I have is If I access any method other than a 'GET' type then I'm receiving the following error in my JSON response: 我遇到的问题是,如果我访问除“ GET”类型以外的任何方法,那么我的JSON响应中将收到以下错误:

The requested resource does not support http method 'GET'

The Code: 编码:

The following code is how the project currently is. 以下代码是项目当前的状态。

RouteConfig.cs class: RouteConfig.cs类:

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute(
            name: "Home",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

The ValuesController controller class: ValuesController控制器类:

public class ValuesController : ApiController
{

    private List<CompanyData> _company;

    public ValuesController()
    {
        _company = new List<CompanyData>
        {
            new CompanyData
            {
                CompanyName = "SmallTech.Ltd",
                CompanyOwner = "John Smith",
                CompanyIndustry = "Electronic Components",
                EmployeeNo = "3"
            }

        };
    }

    public List<CompanyData> GetCompanyData()
    {
        return _company;
    }


     //GET api/values
    public IEnumerable<string> Get()
    {
        return new string[] { "Test GET Method"};
    }

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

    // POST api/values
    public void Post(string value)
    {
        string test = value;
    }

    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    [HttpDelete]
    public void Delete(int id)
    {
    }

An example of calling the above Delete method when the error occurs is: 发生错误时调用上述Delete方法的示例是:

http://testwebapisite.azurewebsites.net/api/values/Delete/5 http://testwebapisite.azurewebsites.net/api/values/Delete/5

I have read other people having the same issue and using the HTTP attributes from the System.Net.MVC . 我已经读过其他人,他们同样有问题并且使用System.Net.MVC的HTTP属性。 I can confirm I'm not using this and am using `System.Net.Http.HttpPostAttribute. 我可以确认我没有使用它,而是在使用System.Net.Http.HttpPostAttribute。

Any help working out why I'm receiving the GET error message would be great. 任何解决为什么我收到GET错误消息的帮助都将非常有用。

You are trying to access an action which clearly specifies delete as its verb via a GET request. 您正在尝试通过GET请求访问明确指定删除为其动词的操作。

By default the browser will do a GET request if you paste a url so thats pretty much easy to test but for the other verbs you'll have to use an actual rest/http client to specify the verb. 默认情况下,如果您粘贴url,浏览器将执行GET请求,这样就很容易测试,但是对于其他动词,则必须使用实际的rest / http客户端来指定动词。 You can use Postman or Rest Console if you use chrome to dev/test 如果您使用chrome进行开发/测试,则可以使用Postman或Rest Console

In addition to those tools, you might want to have fiddler installed .. it will help you track all http activity (both sent/received) you'll know exactly what you are sending and receiving from the wire 除了这些工具之外,您可能还希望安装提琴手 ..它将帮助您跟踪所有http活动(已发送/已接收),您将确切地知道从网络发送和接收的内容

You could also do this from code if you want using HttpClient . 如果您想使用HttpClient也可以从代码中执行此操作。

using (var client = new HttpClient())
            {
                client.BaseAddress = new Uri("http://testwebapisite.azurewebsites.net/");
                client.DefaultRequestHeaders.Accept.Clear();
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));


                   HttpResponseMessage response = await client.DeleteAsync("api/values/5");
                }

You haven't shown the code that you are using to invoke the API, but I suspect you are not using the DELETE HTTP verb. 您尚未显示用于调用API的代码,但我怀疑您没有在使用DELETE HTTP动词。 The resource you are accessing has URI or http://testwebapisite.azurewebsites.net/api/values/5 - note the action name is not specified. 您正在访问的资源具有URI或http://testwebapisite.azurewebsites.net/api/values/5请注意未指定操作名称。 Rather, as the comment of your method suggests, you should be using the DELETE HTTP verb. 相反,正如方法注释所建议的那样,您应该使用DELETE HTTP动词。 Example: 例:

using (var client = new HttpClient())
    await client.DeleteAsync("http://testwebapisite.azurewebsites.net/api/values/5");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#webapi-请求的资源不支持http方法&#39;GET&#39; - c# webapi - The requested resource does not support http method 'GET' WebApi“请求的资源不支持http方法&#39;DELETE” - WebApi “The requested resource does not support http method 'DELETE” 请求的资源不支持HTTP方法GET - The requested resource does not support HTTP method GET 请求的资源不支持 HTTP 方法“GET” - The requested resource does not support HTTP method 'GET' WebApi Post 方法总是返回“请求的资源不支持 http 方法 &#39;GET&#39;。” 状态:405 方法不允许 - WebApi Post Methods always Returns “The requested resource does not support http method 'GET'.” Status: 405 Method Not Allowed WebApi 2 Http Post 405“请求的资源不支持http方法&#39;POST&#39;” - WebApi 2 Http Post 405 “The requested resource does not support http method 'POST'” 所请求的资源在Visual Studio 2010中不支持HTTP方法“ GET” - The requested resource does not support HTTP method 'GET' in visual studio 2010 请求的资源不支持 http 方法“GET”,但使用“POST” - The requested resource does not support http method 'GET' but using 'POST' ASP.Net-请求的资源不支持http方法“ GET” - ASP.Net - Requested resource does not support http method 'GET' 请求的资源不支持http方法&#39;get&#39;。 跨域 - the requested resource does not support http method 'get'. Cross Domain
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM