Пример #1
0
        public void Configure(IApplicationBuilder app)
        {
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/people/{id:int}", async context =>
                {
                    var personId = int.Parse(context.Request.RouteValues["id"].ToString() !);
                    await HalJsonGenerator.HalHandler(context, new Person
                    {
                        Id        = personId,
                        FirstName = "Test",
                        LastName  = "User"
                    });
                });

                endpoints.MapGet("/people/{id:int}/address", async context =>
                {
                    await HalJsonGenerator.HalHandler(context, new Address
                    {
                        FirstLine  = "Some First Line",
                        SecondLine = "Some Second Line"
                    });
                });
            });
        }
Пример #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/people", async context =>
                {
                    var peopleRepo = context.RequestServices.GetRequiredService <PeopleRepository>();
                    var model      = peopleRepo.List(new Paging());
                    await HalJsonGenerator.HalHandler(context, model);
                });

                endpoints.MapGet("/people/{id:int}", async context =>
                {
                    var peopleRepo = context.RequestServices.GetRequiredService <PeopleRepository>();
                    var personId   = int.Parse(context.Request.RouteValues["id"].ToString());
                    var model      = peopleRepo.Get(personId);
                    await HalJsonGenerator.HalHandler(context, model);
                });

                endpoints.MapGet("/people/{id:int}/addresses", async context =>
                {
                    var peopleRepo = context.RequestServices.GetRequiredService <PeopleRepository>();
                    var personId   = int.Parse(context.Request.RouteValues["id"].ToString());
                    var person     = peopleRepo.Get(personId);
                    var addresses  = person.Addresses;
                    var paging     = new Paging();
                    var model      = new PagedList <Address>
                    {
                        CurrentPage = paging.Page,
                        TotalItems  = addresses.Length,
                        TotalPages  = (int)Math.Ceiling(addresses.Length / (double)paging.PageSize),
                        Items       = addresses
                    };
                    await HalJsonGenerator.HalHandler(context, model);
                });

                endpoints.MapGet("/people/{id:int}/addresses/{addressid:int}", async context =>
                {
                    var peopleRepo = context.RequestServices.GetRequiredService <PeopleRepository>();
                    var personId   = int.Parse(context.Request.RouteValues["id"].ToString());
                    var person     = peopleRepo.Get(personId);
                    var addressId  = int.Parse(context.Request.RouteValues["addressid"].ToString());
                    var model      = person.Addresses.FirstOrDefault(x => x.Id == addressId);

                    await HalJsonGenerator.HalHandler(context, model);
                });
            });
        }
Пример #3
0
        public async Task ProducesJsonDocumentWhenHalRepresentationIsNotAvailable()
        {
            var httpContext = CreateHttpContext();
            await HalJsonGenerator.HalHandler(httpContext, new DummyModel
            {
                Id       = 123,
                Property = "Test"
            });

            httpContext.Response.ContentType.Should().Be("application/json");

            var rawJson = await ReadHttpResponseBody(httpContext.Response);

            rawJson.Should().NotBeEmpty();
        }
Пример #4
0
        public async Task UsesJsonSerializerOptionsFromRequestServices()
        {
            // JsonSerializerOptions uses PascalCase by default, Hallo default is camelCase
            _services.AddSingleton(new JsonSerializerOptions());

            var httpContext = CreateHttpContext();
            await HalJsonGenerator.HalHandler(httpContext, new DummyModel
            {
                Id       = 123,
                Property = "Test"
            });

            var rawJson = await ReadHttpResponseBody(httpContext.Response);

            var json = JsonDocument.Parse(rawJson).RootElement;

            json.TryGetProperty("Id", out _).Should().BeTrue();
            json.TryGetProperty("Property", out _).Should().BeTrue();
        }
Пример #5
0
        public async Task ProducesHalJsonDocument()
        {
            _services.AddTransient <Hal <DummyModel>, FullRepresentation>();

            var httpContext = CreateHttpContext();
            await HalJsonGenerator.HalHandler(httpContext, new DummyModel
            {
                Id       = 123,
                Property = "Test"
            });

            httpContext.Response.ContentType.Should().Be("application/hal+json");

            var rawJson = await ReadHttpResponseBody(httpContext.Response);

            var json = JsonDocument.Parse(rawJson).RootElement;

            json.TryGetProperty("_links", out _).Should().BeTrue();
            json.TryGetProperty("_embedded", out _).Should().BeTrue();
        }
Пример #6
0
 /// <inheritdoc cref="SystemTextJsonOutputFormatter"/>
 public override async Task WriteAsync(OutputFormatterWriteContext context)
 {
     await HalJsonGenerator.HalHandler(context.HttpContext, context.Object);
 }