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");
}
}
StartUp.cs
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'."
最佳答案
这里的问题很简单。
您课程的访问修饰符非常严格
class HomeController : ApiController
{
默认访问修饰符是内部的,这意味着它不能公开访问。
尝试将访问修饰符更改为
public
以公开公开服务。public class HomeController : ApiController
{
我可以使用邮递员和现有的Web API复制您的问题。
来自邮递员的错误:
关于c# - WebAPI通过传递参数未在 Controller 上找到任何操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39741123/