Skip to content

Commit

Permalink
AspNetMvcFacility: Make controller lookup case insensitive
Browse files Browse the repository at this point in the history
  • Loading branch information
Yitzchok authored and jonorossi committed Mar 29, 2019
1 parent 94bd301 commit 12e7bc1
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Castle Windsor Changelog

## Unreleased

Bugfixes:
- Finding the controller should be made in a case insensitive way for Castle.Facilities.AspNet.Mvc facility (@yitzchok, #480)

## 5.0.0 (2019-02-12)

Bugfixes:
Expand Down
68 changes: 68 additions & 0 deletions src/Castle.Facilities.AspNet.Mvc.Tests/ControllerFinderTestCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2004-2017 Castle Project - http://www.castleproject.org/
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Castle.Facilities.AspNet.Mvc.Tests
{
using System.Web.Mvc;
using System.Web.Routing;

using Castle.Facilities.AspNet.Mvc;
using Castle.MicroKernel.Lifestyle;
using Castle.MicroKernel.Registration;
using Castle.Windsor;

using NUnit.Framework;

[TestFixture]
public class ControllerFinderTestCase
{
private AspNetMvcFacility facility = null;
private WindsorContainer container;

[SetUp]
public void SetUp()
{
container = new WindsorContainer();

container.AddFacility<AspNetMvcFacility>(x =>
{
facility = x;
x.AddControllerAssembly<MvcTestController>();
});
}

[TestCase("MvcTest")]
[TestCase("mvctest")]
[TestCase("Mvctest")]
[TestCase("MVCtest")]
public void Should_find_controller_by_name_ignoring_case(string controllerName)
{
container.Register(Component.For<MvcTestController>().LifestyleScoped());

var result = ResolveController(controllerName);

Assert.That(typeof(MvcTestController), Is.EqualTo(result.GetType()));
}

private IController ResolveController(string controllerName)
{
using (container.BeginScope())
return facility.ControllerFactory.CreateController(new RequestContext(), controllerName);
}

public class MvcTestController : Controller
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ public class AspNetMvcControllerFactory : IControllerFactory
private readonly IKernel kernel;
private bool autoCreateLifestyleScopes;
private SessionStateBehavior sessionStateBehaviour;
private IEnumerable<Type> controllerCandidateTypes;
private IEnumerable<Type> controllerCandidateTypes;

public event Action<IKernel, Type> BeforeControllerResolved;
public event Action<IKernel, Type> BeforeControllerResolved;
public event Action<IKernel, object> AfterControllerReleased;

public AspNetMvcControllerFactory(IKernel kernel, bool autoCreateLifestyleScopes, SessionStateBehavior sessionStateBehaviour, List<Assembly> controllerAssemblies)
Expand Down Expand Up @@ -100,7 +100,7 @@ private Type FindControllerType(string controllerName)
{
foreach (var type in this.controllerCandidateTypes)
{
if (type.Name == controllerName)
if (type.Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase))
{
return type;
}
Expand All @@ -125,7 +125,5 @@ private void DisposeScopedLifestyleIfRequired()
HttpContext.Current.Items.Remove(ContextScopeKey);
}
}


}
}

0 comments on commit 12e7bc1

Please sign in to comment.