public ContentResult Index() { LoggerStatic.Logger.Debug("Api Index"); var indexPage = new IndexPage { ServiceName = "Api", }; var text = SerializerJson.SerializeObjectToJsonString(indexPage); return(new ContentResult { ContentType = "application/json", Content = text, StatusCode = (int)HttpStatusCode.OK }); }
public async Task <ActionResult <RpGetCitizens> > GetCitizens([FromBody] RqGetCitizens rqGetCitizens) { try { if (!string.IsNullOrEmpty(rqGetCitizens.SearchRequest.Inn) && !ValidationsCollection.Validations.IsValidInnForIndividual(rqGetCitizens.SearchRequest.Inn)) { return(Json(ResponseHelper.ReturnBadRequest("Inn not valid"))); } if (!string.IsNullOrEmpty(rqGetCitizens.SearchRequest.Snils) && !ValidationsCollection.Validations.IsValidSnils(StringConverter.GetNumbers(rqGetCitizens.SearchRequest.Snils))) { return(Json(ResponseHelper.ReturnBadRequest("Snils not valid"))); } var searchRequestParsed = new SearchRequestParsed(); try { searchRequestParsed = CitizenConverter.FromSearchRequestToSearchRequestParsed(rqGetCitizens.SearchRequest); } catch (Exception exception) { LoggerStatic.Logger.Warn("Exception: " + exception); return(Json(ResponseHelper.ReturnBadRequest(exception.Message))); } var rpGetCitizens = new RpGetCitizens { Citizens = await _peopleService.GetCitizens(searchRequestParsed) }; //return Json(rpGetCitizens); return(ControllersHelper.ReturnContentResult(SerializerJson.SerializeObjectToJsonString(rpGetCitizens))); } catch (Exception exception) { LoggerStatic.Logger.Error("Exception: " + exception); return(Json(ResponseHelper.ReturnInternalServerError(exception.Message))); } }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var sqlConnectionString = Configuration.GetConnectionString("DefaultConnection"); services.AddDbContext <PeopleDbContext>( options => options.UseSqlServer(sqlConnectionString), ServiceLifetime.Scoped ); services.AddControllers() .AddJsonOptions(opt => { opt.JsonSerializerOptions.DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull; opt.JsonSerializerOptions.WriteIndented = true; opt.JsonSerializerOptions.Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping; opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); }); services.AddSingleton <IConfiguration>(Configuration); services.AddSwaggerGen(options => { options.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "People API", Description = "A simple example Web API", Contact = new OpenApiContact() { Name = "Name", Email = string.Empty, //Url = "http://test.test" } } ); }); services.ConfigureSwaggerGen(options => { options.CustomSchemaIds(x => x.FullName); }); services.RegisterServices(Configuration); services.Configure <ApiBehaviorOptions>(o => { o.InvalidModelStateResponseFactory = actionContext => { var keyList = string.Join(",", actionContext.ModelState.Keys); return(ControllersHelper.ReturnContentResult(SerializerJson.SerializeObjectToJsonString(ResponseHelper.ReturnBadRequest($"ModelState.Keys: {keyList}")))); }; }); services.AddLogging(builder => { builder.AddConfiguration(Configuration.GetSection("Logging")) .AddConsole() .AddDebug(); }); services.Configure <WSettings>(Configuration); }
public void Configure(IApplicationBuilder app, IWebHostEnvironment env, PeopleDbContext dbContext) { try { dbContext.Database.Migrate(); } catch (Exception exception) { LoggerStatic.Logger.Error("Startup Configure exception: " + exception); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseExceptionHandler( options => { options.Run( async context => { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.ContentType = "application/json"; var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); if (exceptionHandlerFeature != null) { LoggerStatic.Logger.Error("UseExceptionHandler Error: " + exceptionHandlerFeature.Error); var text = SerializerJson.SerializeObjectToJsonString(new ResponseError(ErrorType.InternalError)); await context.Response.WriteAsync(text).ConfigureAwait(false); } }); } ); } else { app.UseExceptionHandler( options => { options.Run( async context => { context.Response.StatusCode = (int)HttpStatusCode.InternalServerError; context.Response.ContentType = "application/json"; var exceptionHandlerFeature = context.Features.Get <IExceptionHandlerFeature>(); if (exceptionHandlerFeature != null) { LoggerStatic.Logger.Error("UseExceptionHandler Error: " + exceptionHandlerFeature.Error); var text = SerializerJson.SerializeObjectToJsonString(new ResponseError(ErrorType.InternalError)); await context.Response.WriteAsync(text).ConfigureAwait(false); } }); } ); } app.UseRouting(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "defaultApi", pattern: "api/{controller}/{action}/{id?}"); } ); app.UseForwardedHeaders(new ForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); app.UseMiddleware(typeof(ErrorHandlingMiddleware)); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "People API V1"); }); }