为什么我的Web API调用返回“在控制器'DPlatypus'上找不到与请求匹配的操作”?

时间:2014-01-14 22:10:37

标签: dependency-injection asp.net-web-api repository castle-windsor asp.net-web-api-routing

我正在尝试创建最简单的ASP.NET Web API应用程序,该应用程序将结合使用DI并与Castle Windsor一起使用。

我有一个更复杂的工作应用程序,我试图从中复制“裸骨”(尽可能少但仍然有效)。

工作应用有此网址(“主页”):

http://localhost:28642/

输入:

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

...在我的浏览器中从Departments Controller返回我想要的数据。

但是,新/简单/不工作的应用程序具有此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);
    }

}

这是Repository类:

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,这样它们就像工作应用程序一样,但是......不行。为什么会这样?

更新

对于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

Kiran 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>

1 个答案:

答案 0 :(得分:2)

更改操作的路径模板,如下所示,否则将从查询字符串中获得值id。即http://localhost:33181/api/DPlatypus?id=4会起作用。所以你需要修改你的模板。

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