MVC3 + Ninject: What is the proper way to inject t

2019-06-17 07:53发布

I have seen the following two example for injecting the User IPrincipal:

Method 1:

kernel.Bind<IPrincipal>()
   .ToMethod(context => context.Kernel.Get<RequestContext>().HttpContext.User)
   .InRequestScope();

Method 2:

kernel.Bind<IPrincipal>()
  .ToMethod(context => HttpContext.Current.User)
  .InRequestScope();

Is there any difference in the two? Is one preferred?

1条回答
老娘就宠你
2楼-- · 2019-06-17 08:29

The two methods are identical. Both are going to return the HttpContext obect for the current HTTP Request.

查看更多
登录 后发表回答