GithubHelp home page GithubHelp logo

Comments (3)

cosullivan avatar cosullivan commented on July 19, 2024

Hi,

The easiest way to do this at the moment is to write your own JsonApiMediaTypeFormatter and modify the JsonValue that is being returned. The JsonValue is simply an AST that you can then modify in anyway you choose.

See the following example;
https://github.com/cosullivan/Hypermedia/blob/master/Src/Hypermedia.Sample.WebApi/Services/JsonApiMetadataMediaTypeFormatter.cs

Thanks,
Cain.

from hypermedia.

 avatar commented on July 19, 2024

Ok, I can add metadata such as time and version. I make pagination now. How to add the total number of records to metadata? There is method of GetPostsController in Hypermedia.Sample.WebApi project:
`[HttpGet, Route("v1/posts")]
public IHttpActionResult Execute(string q = null, int skip = 0, int take = 100, IRequestMetadata metadata = null)
{
var posts = _database.Posts.GetAll(Predicate(q), skip, take).AsResource();

        foreach (var post in posts)
        {
            post.Populate(_database);
        }

        return Ok(posts);
    }`

Can you show me how add the total number of records to metadata on this example?
Thank you.

from hypermedia.

cosullivan avatar cosullivan commented on July 19, 2024

Hi,

There is no easy way to attach metadata to the request, but I would suggest the easist way is to attach your own properties to the Request message (ie, Request.Properties.Add(...) from within the controller) and then read these out during the media type formatting stage.

The key here is to take note of the "GetPerRequestFormatterInstance" method that gets called on the formatter as this has the request message passed to it.

Thanks,
Cain.

from hypermedia.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.