asp.net-mvc – 在我自己的OWIN中间件中使用Ninject DI
发布时间:2020-12-16 07:41:07 所属栏目:asp.Net 来源:网络整理
导读:我已经制作了一个简单的OWIN中间件,它将为我提供一个User对象并将其添加到HttpContext.Current.Items中,以便每个请求的所有控制器和视图都可用. 这是我的代码: public class SetCurrentUserMiddleware : OwinMiddleware{ public SetCurrentUserMiddleware(O
我已经制作了一个简单的OWIN中间件,它将为我提供一个User对象并将其添加到HttpContext.Current.Items中,以便每个请求的所有控制器和视图都可用.
这是我的代码: public class SetCurrentUserMiddleware : OwinMiddleware { public SetCurrentUserMiddleware(OwinMiddleware next) : base(next) { } public override Task Invoke(IOwinContext context) { if (context.Request.User.Identity.IsAuthenticated) { // Do some work to get a userId... (omitted) var repo = new UserRepository(); User user = repo.Get(userId); HttpContext.Current.Items["CurrentUserContext"] = user; } return Next.Invoke(context); } } 我在我的Web应用程序中使用Ninject – 如何重构这个中间件,以便将我的UserRepository注入为依赖项?这有可能吗? 解决方法
根据
this page,您可以提供自己的构造函数参数.
public class SetCurrentUserMiddleware : OwinMiddleware { private readonly IUserRepository userRepository; public SetCurrentUserMiddleware(OwinMiddleware next,IUserRepository userRepository) : base(next) { if (userRepository == null) throw new ArgumentNullException("userRepository"); this.userRepository = userRepository; } public override Task Invoke(IOwinContext context) { if (context.Request.User.Identity.IsAuthenticated) { // Do some work to get a userId... (omitted) User user = this.userRepository.Get(userId); HttpContext.Current.Items["CurrentUserContext"] = user; } return Next.Invoke(context); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – 如何在没有代码隐藏文件的情况下在VB.NET aspx页
- asp.net-mvc – ASP.NET MVC – 动态授权
- asp.net – 免费DotNetNuke皮肤资源
- asp.net 读取xml文件里面的内容,绑定到dropdownlist中
- asp.net-core – 恢复AspNet标识核心包时出错
- 如何在asp.net mvc中的html.actionlink中调用javascript函数
- ASP.Net中的Amazon CloudFront失效
- asp.net-mvc – 每个验证属性的所有不显眼的验证属性的列表
- asp.net – 用于.NET的HTML Sanitizer
- 如何默认使用IE来测试我的ASP.NET应用程序?
推荐文章
站长推荐
- asp.net-mvc – ASP.NET MVC 6中的基本视图页面
- .net core入门-发布及部署_异常(处理程序“aspNe
- asp.net – iis 7.0,模块订单更改
- IIS / .NET上的Blackberry推送服务启动器
- ASP.NET ListView – 渲染THEAD / TBODY标签
- asp.net-mvc – 如何使用ASP.NET MVC 1.0从文件系
- asp.net-mvc-3 – DropDownListFor – 不选择“S
- 将asp.net应用程序移植到天蓝色有多大挑战?
- asp.net-mvc – 遵循视图模型模式的最佳实践
- 详解ASP.NET MVC 利用Razor引擎生成静态页
热点阅读