Question

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 Hostname HTTP 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>
Was it helpful?

Solution

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top