Example #1
0
 /// <summary>
 /// ValuesController
 /// </summary>
 /// <param name="blogArticleServices"></param>
 /// <param name="mapper"></param>
 /// <param name="advertisementServices"></param>
 /// <param name="love"></param>
 /// <param name="roleModulePermissionServices"></param>
 /// <param name="user"></param>
 public ValuesController(IherbalifeArticleServices blogArticleServices, IMapper mapper, IAdvertisementServices advertisementServices, Love love, IRoleModulePermissionServices roleModulePermissionServices, IUser user)
 {
     // 测试 Authorize 和 mapper
     _mapper = mapper;
     _advertisementServices = advertisementServices;
     _love = love;
     _roleModulePermissionServices = roleModulePermissionServices;
     // 测试 Httpcontext
     _user = user;
     // 测试AOP加载顺序,配合 return
     _blogArticleServices = blogArticleServices;
 }
Example #2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="next"></param>
 /// <param name="blogArticleServices"></param>
 public RequRespLogMildd(RequestDelegate next, IherbalifeArticleServices blogArticleServices)
 {
     _next = next;
     _blogArticleServices = blogArticleServices;
 }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, IherbalifeArticleServices _blogArticleServices)
        {
            #region ReuestResponseLog

            if (Appsettings.app("AppSettings", "Middleware_RequestResponse", "Enabled").ObjToBool())
            {
                app.UseReuestResponseLog();//记录请求与返回数据
            }

            #endregion

            #region Environment
            if (env.IsDevelopment())
            {
                // 在开发环境中,使用异常页面,这样可以暴露错误堆栈信息,所以不要放在生产环境。
                app.UseDeveloperExceptionPage();

                //app.Use(async (context, next) =>
                //{
                //    //这里会多次调用,这里测试一下就行,不要打开注释
                //    //var blogs =await _blogArticleServices.Getherbalifes();
                //    var processName = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
                //    Console.WriteLine(processName);
                //    await next();
                //});
            }
            else
            {
                app.UseExceptionHandler("/Error");
                // 在非开发环境中,使用HTTP严格安全传输(or HSTS) 对于保护web安全是非常重要的。
                // 强制实施 HTTPS 在 ASP.NET CRS,配合 app.UseHttpsRedirection
                //app.UseHsts();
            }
            #endregion

            #region Swagger
            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                //根据版本名称倒序 遍历展示
                typeof(ApiVersions).GetEnumNames().OrderByDescending(e => e).ToList().ForEach(version =>
                {
                    c.SwaggerEndpoint($"/swagger/{version}/swagger.json", $"{ApiName} {version}");
                });
                // 将swagger首页,设置成我们自定义的页面,记得这个字符串的写法:解决方案名.index.html
                c.IndexStream = () => GetType().GetTypeInfo().Assembly.GetManifestResourceStream("herbalife.CRS.index.html"); //这里是配合MiniProfiler进行性能监控的,《文章:完美基于AOP的接口性能分析》,如果你不需要,可以暂时先注释掉,不影响大局。
                c.RoutePrefix = "";                                                                                           //路径配置,设置为空,表示直接在根域名(localhost:8001)访问该文件,注意localhost:8001/swagger是访问不到的,去launchSettings.json把launchUrl去掉,如果你想换一个路径,直接写名字即可,比如直接写c.RoutePrefix = "doc";
            });
            #endregion

            #region MiniProfiler
            app.UseMiniProfiler();
            #endregion

            #region 第三步:开启认证中间件

            //此授权认证方法已经放弃,请使用下边的官方验证方法。但是如果你还想传User的全局变量,还是可以继续使用中间件,第二种写法//app.UseMiddleware<JwtTokenAuth>();
            app.UseJwtTokenAuth();

            //如果你想使用官方认证,必须在上边ConfigureService 中,配置JWT的认证服务 (.AddAuthentication 和 .AddJwtBearer 二者缺一不可)
            app.UseAuthentication();
            #endregion

            #region CORS
            //跨域第二种方法,使用策略,详细策略信息在ConfigureService中
            app.UseCors("LimitRequests");//将 CORS 中间件添加到 web 应用程序管线中, 以允许跨域请求。


            #region 跨域第一种版本
            //跨域第一种版本,请要ConfigureService中配置服务 services.AddCors();
            //    app.UseCors(options => options.WithOrigins("http://localhost:8021").AllowAnyHeader()
            //.AllowAnyMethod());
            #endregion

            #endregion


            // 跳转https
            //app.UseHttpsRedirection();
            // 使用静态文件
            app.UseStaticFiles();
            // 使用cookie
            app.UseCookiePolicy();
            // 返回错误码
            app.UseStatusCodePages();//把错误码返回前台,比如是404


            app.UseMvc();


            app.UseSignalR(routes =>
            {
                //这里要说下,为啥地址要写 /api/xxx
                //因为我前后端分离了,而且使用的是代理模式,所以如果你不用/api/xxx的这个规则的话,会出现跨域问题,毕竟这个不是我的controller的路由,而且自己定义的路由
                routes.MapHub <ChatHub>("/api2/chatHub");
            });
        }
Example #4
0
 // 这里可以注入
 public Job1TimedService(IherbalifeArticleServices blogArticleServices)
 {
     _blogArticleServices = blogArticleServices;
 }
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="blogArticleServices"></param>
 /// <param name="redisCacheManager"></param>
 public herbalifeController(IherbalifeArticleServices blogArticleServices, IRedisCacheManager redisCacheManager)
 {
     _blogArticleServices = blogArticleServices;
     _redisCacheManager   = redisCacheManager;
 }