C# WebAPI未通过传递参数在控制器上找到任何操作

C# WebAPI未通过传递参数在控制器上找到任何操作,c#,asp.net-web-api,C#,Asp.net Web Api,HomeController.cs class HomeController : ApiController { [HttpGet] public IHttpActionResult GetData(string name) { if (string.IsNullOrWhiteSpace(name)) { throw new ArgumentNullExcep

HomeController.cs

 class HomeController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetData(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("username can not be empty");
            }
            return Ok("Test Done");
        }
    }
public void Configuration(IAppBuilder appBuilder)
        {           
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();


            config.Routes.MapHttpRoute(
              name: "GetData",
              routeTemplate: "V2/{controller}/{name}",
              defaults: new { controller = "Home", action = "GetData" }
              );

            appBuilder.UseWebApi(config);
        }
StartUp.cs

 class HomeController : ApiController
    {
        [HttpGet]
        public IHttpActionResult GetData(string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("username can not be empty");
            }
            return Ok("Test Done");
        }
    }
public void Configuration(IAppBuilder appBuilder)
        {           
            HttpConfiguration config = new HttpConfiguration();
            config.MapHttpAttributeRoutes();


            config.Routes.MapHttpRoute(
              name: "GetData",
              routeTemplate: "V2/{controller}/{name}",
              defaults: new { controller = "Home", action = "GetData" }
              );

            appBuilder.UseWebApi(config);
        }
获取错误:

{
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost:4057/V2/Home/GetData/'.",
  "MessageDetail": "No type was found that matches the controller named 'Home'."
[RoutePrefix("V2/Home")]
public class HomeController : ApiController
{
    [HttpGet]
    [Route("{name}", Name = "GetData")]
    public IHttpActionResult GetData(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            throw new ArgumentNullException("username can not be empty");
        }
        return Ok("Test Done");
    }
}
public void Configuration(IAppBuilder appBuilder)
{
    HttpConfiguration config = new HttpConfiguration();
    config.MapHttpAttributeRoutes();
    appBuilder.UseWebApi(config);
}

尝试将方法名称更改为
Get
(有关详细信息)


如果您不想更改方法名称(),设置属性路由可能会有所帮助。

这里的问题很简单。 类的访问修饰符非常严格

 class HomeController : ApiController
    {
默认的访问修饰符是内部的,这意味着它不能公开访问

尝试将访问修饰符更改为
public
以公开服务

public class HomeController : ApiController
        {
我可以使用postman和我现有的web API复制您的问题

我的邮递员的错误:

因为,API控制器必须是公共的。但是,考虑在常规路由上使用属性路由,因为它更容易出错:

HomeController.cs:

{
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost:4057/V2/Home/GetData/'.",
  "MessageDetail": "No type was found that matches the controller named 'Home'."
[RoutePrefix("V2/Home")]
public class HomeController : ApiController
{
    [HttpGet]
    [Route("{name}", Name = "GetData")]
    public IHttpActionResult GetData(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            throw new ArgumentNullException("username can not be empty");
        }
        return Ok("Test Done");
    }
}
public void Configuration(IAppBuilder appBuilder)
{
    HttpConfiguration config = new HttpConfiguration();
    config.MapHttpAttributeRoutes();
    appBuilder.UseWebApi(config);
}
Startup.cs:

{
  "Message": "No HTTP resource was found that matches the request URI 'http://localhost:4057/V2/Home/GetData/'.",
  "MessageDetail": "No type was found that matches the controller named 'Home'."
[RoutePrefix("V2/Home")]
public class HomeController : ApiController
{
    [HttpGet]
    [Route("{name}", Name = "GetData")]
    public IHttpActionResult GetData(string name)
    {
        if (string.IsNullOrWhiteSpace(name))
        {
            throw new ArgumentNullException("username can not be empty");
        }
        return Ok("Test Done");
    }
}
public void Configuration(IAppBuilder appBuilder)
{
    HttpConfiguration config = new HttpConfiguration();
    config.MapHttpAttributeRoutes();
    appBuilder.UseWebApi(config);
}

[Routing]
属性
可能会有所帮助。方法的
名称
不是这里的问题。由于他使用的是
[HttpGet]
,因此不会有任何问题。另外,路由注释仅在特定版本的MVC中可用,我认为它是在MVC 5中引入的,它是在WebAPI 2中引入的。@codran Yes,他之所以使用它,是因为他的代码中出现了
IHttpActionResult
,对吗?@MartinBrandl确实给出了它,就像
config.maphttpAttribute路由的用户一样:)