为什么我的Web API调用返回“没有行动控制器‘DPlatypus’匹配请求上找到”?(Why i

2019-10-29 17:45发布

我试图创建一个集成和使用DI与温莎城堡最简单可行的ASP.NET Web API的应用程序。

我有一个工作的应用程序,是比较复杂的,我曾尝试以“裸露的骨头”从复制(尽量少,而且还有它的工作)。

工作程序有这个URL(“主页”):

http://localhost:28642/

进入这个:

http://shannon2:28642/api/Departments/Count

......在我的浏览器返回我想要的数据,从部门控制。

然而,新的/简单/非工作应用程序有这个URL(“主页”):

http://localhost:33181/

...并进入这个:

http://localhost:33181/api/DPlatypus/4

...不回我想要的数据:

事实上,这样的:

`http://shannon2:33181/api/DPlatypus/4` 

...的回报:

错误的请求-无效的主机名HTTP错误400请求主机名无效。

虽然前面显示的URI( http://localhost:33181/api/DPlatypus/4 )返回:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Message>
No HTTP resource was found that matches the request URI `'http://localhost:33181/api/DPlatypus/4'.`
</Message>
<MessageDetail>
No action was found on the controller 'DPlatypus' that matches the request.
</MessageDetail>
</Error>
</MessageDetail>
</Error>

有可以匹配的要求,即一个动作:

public class DPlatypusController : ApiController
{
    private readonly IDPlatypusRepository _duckbillRepository;

    public DPlatypusController(IDPlatypusRepository duckbillRepository)
    {
        if (duckbillRepository == null)
        {
            throw new ArgumentNullException("duckbillRepository is null");
        }
        _duckbillRepository = duckbillRepository;
    }

    [Route("api/DPlatypus")]
    public string GetDPlatypusNameById(int Id)
    {
        return _duckbillRepository.Get(Id);
    }

}

这里是仓库类:

public class DPlatypusRepository : IDPlatypusRepository
{
    private List<DPlatypus> platypi = new List<DPlatypus>();
    private int _nextId = 1;

    public DPlatypusRepository()
    {
        Add(new DPlatypus { Name = "Donald" });
        Add(new DPlatypus { Name = "GoGo" });
        Add(new DPlatypus { Name = "Patty" });
        Add(new DPlatypus { Name = "Platypup" });
        Add(new DPlatypus { Name = "Platypop" });
        Add(new DPlatypus { Name = "Platymop" });
        Add(new DPlatypus { Name = "Platydude" });
        Add(new DPlatypus { Name = "Platydudette" });
    }

    public IEnumerable<DPlatypus> GetAll()
    {
        return platypi;
    }

    public String Get(int id)
    {
        return platypi.Find(p => p.Id == id).Name;
    }

    public DPlatypus Add(DPlatypus platypus)
    {
        if (platypus == null)
        {
            throw new ArgumentNullException("platypus");
        }
        platypus.Id = _nextId++;
        platypi.Add(platypus);
        return platypus;
    }
}

我注册/在WindsorDependencyResolver.cs安装,我已经改变的Global.asax,和WebApiConfig.cs,使它们就像工作的应用程序,但...没有去。 这可能是为什么?

UPDATE

对于M. Mimpen,这里是Global.asax中:

namespace WebApplication4
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        private static IWindsorContainer _container;
        protected void Application_Start()
        {
            ConfigureWindsor(GlobalConfiguration.Configuration);
            GlobalConfiguration.Configure(c => WebApiConfig.Register(c, _container));

            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        public static void ConfigureWindsor(HttpConfiguration configuration)
        {
            _container = new WindsorContainer();
            _container.Install(FromAssembly.This());
            _container.Kernel.Resolver.AddSubResolver(new CollectionResolver(_container.Kernel, true));
            var dependencyResolver = new WindsorDependencyResolver(_container);
            configuration.DependencyResolver = dependencyResolver;
        }

        protected void Application_End()
        {
            _container.Dispose();
            base.Dispose();
        }

    }
}

更新2

对于基兰Challa; 现在我得到:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost:33181/api/DPlatypus?id=4'.
</Message>
<MessageDetail>
No action was found on the controller 'DPlatypus' that matches the request.
</MessageDetail>
</Error>

Answer 1:

变化像下面另有价值的行动的路径模板id将从查询字符串可以预期的。 即http://localhost:33181/api/DPlatypus?id=4会工作。 所以,你需要修改你的模板。

[Route("api/DPlatypus/{id}")]
public string GetDPlatypusNameById(int Id)


文章来源: Why is my Web API call returning “No action was found on the controller 'DPlatypus' that matches the request”?