gpt4 book ai didi

c# - WebAPI 通过传递参数未在 Controller 上找到任何操作

转载 作者:行者123 更新时间:2023-11-30 23:20:28 37 4
gpt4 key购买 nike

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
{

我可以使用 postman 和我现有的网络 API 来复制您的问题。

My API Controller

来 self 的 postman 的错误: enter image description here

关于c# - WebAPI 通过传递参数未在 Controller 上找到任何操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39741123/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com