简体   繁体   中英

Asp.net Web api The requested resource does not support http method 'GET'

I want to build a Web API which accepts a string parameter and return a string value,but I always get the error, The requested resource does not support HTTP method GET . I searched a bit in the stack overflow and added the line [AcceptVerbs("GET")] but I still got the error. Could any one help me out?

Global.asax.cs file

 config.Routes.MapHttpRoute(
                name: "sasRoute",
                routeTemplate: "api/sas/{container}",
                defaults: new
                {
                    controller = "sas",
                    container = RouteParameter.Optional
                }
            );

Controller's method

[Route("{container}")]
[AcceptVerbs("GET")]
[HttpGet]
public string GetContainerToken(string container)
{
    return container;
}

You do not need to register Custom Route , as you already have Attribute Route .

Remove it from your code. Instead, you will need RoutePrefix .

[RoutePrefix("api/sas")]
public class SasController : ApiController
{
    [Route("{container}")]
    public string Get(string container)
    {
        return container;
    }
}

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