Ограничение маршрута с помощью Ninject и dbcontext - PullRequest
0 голосов
/ 19 марта 2012

Я пытаюсь создать ограничение, которое проверяет базу данных.И я использую Ninject, но по какой-то причине он не создает новый экземпляр моего хранилища при запуске.

global.asax.cs

// Content
routes.MapRoute(
        "Content Language Route",
        "{languageID}/List",
             new { controller = "Content", action = "Index",
             new { languageID = new LanguageRouteConstraint() },
             new string[] { "MyProj.MVC.Controllers" }
         ); 
.....
kernel.Bind<IContentRepository>().To<ContentRepository>();

Ограничение

public class LanguageRouteConstraint : IRouteConstraint
{
#region IRouteConstraint Members

private readonly IContentRepository _contentRepository;

public LanguageRouteConstraint(IContentRepository contentRepository)
{
     this._contentRepository = contentRepository;
}

public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
 if (routeDirection == RouteDirection.IncomingRequest)
 {
   string languageID = values["languageID"].ToString();

   if (String.IsNullOrEmpty(languageID))
     return false;

   MyProj.MVC.Models.Language language = _contentRepository.GetLanguage(languageID);

   return (language != null);
   }
  return false;
  }    
#endregion
}

Использование Ninject для репозитория работает в контроллере, но нужно ли изменить маршрут в gobal asa, чтобы он работал?

1 Ответ

0 голосов
/ 19 марта 2012

Решил так:

 // Content
 routes.MapRoute("Content Language Route",
      "{languageID}/List",
      new { controller = "Content", action = "Index",
      new
      {
      languageID = new LanguageRouteConstraint(
            DependencyResolver.Current.GetService<IContentRepository>())
      },
     new string[] { "MyProj.MVC.Controllers" }
 );
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...