lab*_*roo 4 c# asp.net-mvc ninject asp.net-mvc-4
在一个简单的mvc 4应用程序中,我安装了Ninject.MVC3 nuget包.
这是我的控制器,非常基本,ISomeClass由ninject注入构造函数中.
public class HomeController : Controller
{
private readonly ISomeClass _someClass;
public HomeController(ISomeClass someclass)
{
_someClass = someclass;
}
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public PartialViewResult MiniView()
{
return PartialView("miniview", _someClass.GetName());
}
}
Run Code Online (Sandbox Code Playgroud)
这是SomeClass
public class SomeClass : ISomeClass
{
private readonly string _someName;
public SomeClass(string someName)
{
_someName = someName;
}
public string GetName()
{
return _someName;
}
}
Run Code Online (Sandbox Code Playgroud)
在Index.cshtml视图中我有
@{ Html.RenderAction("MiniView","Home"); }
Run Code Online (Sandbox Code Playgroud)
现在在NinjectWebCommon中,当我去注册服务时,我需要知道请求是否是子动作请求.就像我打电话一样Html.RenderAction.这是我正在尝试但它不起作用.
kernel.Bind<ISomeClass>().To<SomeClass>()
.WithConstructorArgument("someName", c => IsChildAction(c) ? "Child" : "Nope");
Run Code Online (Sandbox Code Playgroud)
IsChildAction方法 - 始终返回false.
private static bool IsChildAction(Ninject.Activation.IContext c)
{
var handler = HttpContext.Current.Handler;
/*Cant do this, ChildActionMvcHandler is internal*/
return handler is System.Web.Mvc.Html.ChildActionExtensions.ChildActionMvcHandler;
//OR
//This is how ControllerContext.IsChildAction gets its value in System.Web.Mvc but
//RouteData.DataTokens is empty for me
return ((MvcHandler)handler).RequestContext.RouteData.DataTokens
.ContainsKey("ParentActionViewContext");
}
Run Code Online (Sandbox Code Playgroud)
任何想法,如果可以做到这一点?
ps:这不是实际的代码,只是尝试一些东西.这是我应该肯定不会做的事情吗?为什么?
我最后检查当前请求是否有前一个处理程序.似乎它只是针对儿童行为设置的.
HttpContext.Current.PreviousHandler != null &&
HttpContext.Current.PreviousHandler is MvcHandler;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1884 次 |
| 最近记录: |