ASP.NET MVC自定义路由约束和依赖注入 [英] ASP.NET MVC Custom Route Constraints and Dependency Injection
本文介绍了ASP.NET MVC自定义路由约束和依赖注入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的ASP.NET MVC 3应用程序,我已经定义如下图所示的路径约束:
公共类CountryRouteConstraint:IRouteConstraint { 私人只读ICountryRepository<国家> _countryRepo; 公共CountryRouteConstraint(ICountryRepository<国家> countryRepo){
_countryRepo = countryRepo;
} 公共BOOL匹配(HttpContextBase HttpContext的,路由路径,字符串参数名称,RouteValueDictionary价值,RouteDirection routeDirection){ //做数据库查询这里 //根据你从数据库得到的值返回结果
返回true;
}
}
我使用Ninject作为我的应用程序的IoC容器,它实现的IDependencyResolver
和我登记我的依赖性:
私有静态无效RegisterServices(内核的iKernel){ kernel.Bind< ICountryRepository<国家>>()。
到< CountryRepository>();
}
我如何使用这条路线的约束与依赖注入友好的方式?
修改
我不能找到一种方法,通过对单元测试这种依赖关系:
[事实]
公共无效country_route_should_pass(){ VAR mockContext =新的模拟< HttpContextBase>();
mockContext.Setup(C => c.Request.Ap prelativeCurrentExecutionFilePath).Returns(〜/国家/意大利); VAR路线=新RouteCollection();
TugberkUgurlu.ReservationHub.Web.Routes.RegisterRoutes(路线); 的RouteData的RouteData = routes.GetRouteData(mockContext.Object); Assert.NotNull(的RouteData);
Assert.Equal(国家,routeData.Values [控制器]);
Assert.Equal(指数,routeData.Values [行动]);
Assert.Equal(意大利,routeData.Values [国]);
}
解决方案
routes.MapRoute(
国家
国家/ {}国家,
新{
控制器=国家
行动=索引
},
新{
国家=新CountryRouteConstraint(
DependencyResolver.Current.GetService< ICountryRepository<国家>>()
)
}
);
On my ASP.NET MVC 3 App, I have a route constraint defined like below:
public class CountryRouteConstraint : IRouteConstraint {
private readonly ICountryRepository<Country> _countryRepo;
public CountryRouteConstraint(ICountryRepository<Country> countryRepo) {
_countryRepo = countryRepo;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection) {
//do the database look-up here
//return the result according the value you got from DB
return true;
}
}
I am using Ninject as IoC container on my app which implements IDependencyResolver
and I registered my dependency:
private static void RegisterServices(IKernel kernel) {
kernel.Bind<ICountryRepository<Country>>().
To<CountryRepository>();
}
How can I use this route constraint with a dependency injection friendly manner?
EDIT
I cannot find a way to pass this dependency on unit test:
[Fact]
public void country_route_should_pass() {
var mockContext = new Mock<HttpContextBase>();
mockContext.Setup(c => c.Request.AppRelativeCurrentExecutionFilePath).Returns("~/countries/italy");
var routes = new RouteCollection();
TugberkUgurlu.ReservationHub.Web.Routes.RegisterRoutes(routes);
RouteData routeData = routes.GetRouteData(mockContext.Object);
Assert.NotNull(routeData);
Assert.Equal("Countries", routeData.Values["controller"]);
Assert.Equal("Index", routeData.Values["action"]);
Assert.Equal("italy", routeData.Values["country"]);
}
解决方案
routes.MapRoute(
"Countries",
"countries/{country}",
new {
controller = "Countries",
action = "Index"
},
new {
country = new CountryRouteConstraint(
DependencyResolver.Current.GetService<ICountryRepository<Country>>()
)
}
);
这篇关于ASP.NET MVC自定义路由约束和依赖注入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文