本文介绍了为什么我的Web API调用返回[否行动控制器“DPlatypus”该请求&QUOT匹配上找到;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我有一个工作程序是比较复杂的,我曾尝试以裸露的骨头从复制(尽量少,仍然有它的工作)。

工作的应用程序有这个URL(主页):

 的http://本地主机:28642 /

输入此:

 的http:// shannon2:28642 / API /部门/计数

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

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

 的http://本地主机:33181 /

...并进入这样的:

 的http://本地主机:33181 / API / DPlatypus / 4

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

其实,这样的:

 `HTTP:// shannon2:33181 / API / DPlatypus / 4`

......返回:

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

虽然URI previously所示(的http://本地主机:33181 / API / DPlatypus / 4 )返回:

 此XML文件没有出现与之相关联的任何样式信息。文档树如下所示。
<错误>
<消息>
没有HTTP资源发现,请求URI`'匹配的http://本地主机:33181 / API / DPlatypus / 4'.`
< /信息>
< MessageDetail>
没有采取任何控制器DPlatypus与请求匹配上找到。
< / MessageDetail>
< /错误>
< / MessageDetail>
< /错误>

但我的的有应匹配的要求,即一个动作:

 公共类DPlatypusController:ApiController
{
    私人只读IDPlatypusRepository _duckbillRepository;    公共DPlatypusController(IDPlatypusRepository duckbillRepository)
    {
        如果(duckbillRepository == NULL)
        {
            抛出新的ArgumentNullException(duckbillRepository为空);
        }
        _duckbillRepository = duckbillRepository;
    }    [路线(API / DPlatypus)]
    公共字符串GetDPlatypusNameById(INT标识)
    {
        返回_duckbillRepository.Get(ID);
    }}

下面是仓库类:

 公共类DPlatypusRepository:IDPlatypusRepository
{
    私人列表< D​​Platypus> platypi =新的List< D​​Platypus>();
    私人INT _nextID = 1;    公共DPlatypusRepository()
    {
        加入(新DPlatypus {名称=唐纳德});
        加入(新DPlatypus {名称=勾勾});
        加入(新DPlatypus {名称=侯佩岑});
        加入(新DPlatypus {名称=Platypup});
        加入(新DPlatypus {名称=Platypop});
        加入(新DPlatypus {名称=Platymop});
        加入(新DPlatypus {名称=Platydude});
        加入(新DPlatypus {名称=Platydudette});
    }    公共IEnumerable的< D​​Platypus>得到所有()
    {
        返回platypi;
    }    公共字符串GET(INT ID)
    {
        返回platypi.Find(P => p.Id == ID).Name点;
    }    公共DPlatypus添加(DPlatypus鸭嘴兽)
    {
        如果(鸭嘴兽== NULL)
        {
            抛出新的ArgumentNullException(鸭嘴兽);
        }
        platypus.Id = _nextID ++;
        platypi.Add(鸭嘴兽);
        返回鸭嘴兽;
    }
}

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

更新

有关M. Mimpen,这里是Global.asax中:

 命名空间WebApplication4
{
    公共类WebApiApplication:System.Web.HttpApplication
    {
        私有静态IWindsorContainer _container;
        保护无效的Application_Start()
        {
            ConfigureWindsor(GlobalConfiguration.Configuration);
            GlobalConfiguration.Configure(C => WebApiConfig.Register(C,_container));            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
        }        公共静态无效ConfigureWindsor(HttpConfiguration配置)
        {
            _container =新WindsorContainer();
            _container.Install(FromAssembly.This());
            _container.Kernel.Resolver.AddSubResolver(新CollectionResolver(_​​container.Kernel,真实));
            VAR dependencyResolver =新WindsorDependencyResolver(_​​container);
            configuration.DependencyResolver = dependencyResolver;
        }        保护无效Application_End()
        {
            _container.Dispose();
            base.Dispose();
        }    }
}

更新2

有关基兰Challa;现在我得到:

 此XML文件没有出现与之相关联的任何样式信息。文档树如下所示。
<错误>
<消息>
没有HTTP资源发现,请求URI相匹配的http://本地主机:33181 / API / DPlatypus ID = 4'。
< /信息>
< MessageDetail>
没有采取任何控制器DPlatypus与请求匹配上找到。
< / MessageDetail>
< /错误>


解决方案

更改操作的路径模板类似下面另有值 ID 将被从查询字符串的预期。即的http://本地主机:33181 / API / DPlatypus ID = 4 会工作。所以,你需要修改你的模板。

  [路线(API / DPlatypus /(编号))
公共字符串GetDPlatypusNameById(INT标识)

I am trying to create the simplest possible ASP.NET Web API app that incorporates and uses DI with Castle Windsor.

I have a working app that is more complicated, and I have tried to copy the "bare bones" from it (as little as possible and still have it work).

The working app has this URL ("Home Page"):

http://localhost:28642/

Entering this:

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

...in my browser returns the data I want, from the Departments Controller.

However, the new/simple/non-working app has this URL ("Home Page"):

http://localhost:33181/

...and entering this:

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

...does NOT return the data I want:

In fact, this:

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

...returns:

Bad Request - Invalid HostnameHTTP Error 400. The request hostname is invalid.

While the URI previously shown (http://localhost:33181/api/DPlatypus/4) returns:

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>

But I do have an action that should match the request, namely:

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);
    }

}

Here is the Repository class:

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;
    }
}

I am registering/installing in WindsorDependencyResolver.cs, I have altered Global.asax, and WebApiConfig.cs, so that they are just like the working app, yet...no go. Why might this be?

UPDATE

For M. Mimpen, here is 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();
        }

    }
}

UPDATE 2

For Kiran Challa; now I get:

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>
解决方案

Change your action's route template like the following otherwise the value id would be expected from the query string. i.e http://localhost:33181/api/DPlatypus?id=4 would work. So you need to modify your template.

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

这篇关于为什么我的Web API调用返回[否行动控制器“DPlatypus”该请求&QUOT匹配上找到;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 22:15