Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing Custom Formatters #139

Closed
danroth27 opened this issue Apr 21, 2015 · 9 comments
Closed

Writing Custom Formatters #139

danroth27 opened this issue Apr 21, 2015 · 9 comments
Assignees

Comments

@danroth27
Copy link
Member

ASP.NET MVC -> Models -> Writing Custom Formatters

@danroth27 danroth27 added this to the Backlog milestone Dec 15, 2015
@danroth27 danroth27 added the TOC label May 19, 2016
@Luis-Palacios
Copy link
Contributor

Luis-Palacios commented Sep 7, 2016

any updates or draft for this? i am currently trying to write a text/javascript formatter some docs will be helpful

@danroth27
Copy link
Member Author

Not yet - still on the backlog.

@Rick-Anderson
Copy link
Contributor

@danroth27 can we get an outline of what you want? I'm looking for a community contribution for this. What are the top use cases? I'm guessing text/javascript formatter would be a good example.

@Rick-Anderson
Copy link
Contributor

Rick-Anderson commented Jan 30, 2017

@danroth27 we really need some guidance ASAP
from @fiyazbinhasan
I managed to write some custom formatters. It was tough cause already all of them are pretty much written by other community members :P. Anyways I wrote a YAML formatter (Input/Output Formatters), Pdf formatter (Output), Xls Formatter (Output). Here is the link to the git repo where you can find them,

I'll write a blog soon. Will keep you notifying. Meanwhile please do check the repo and let me know if I missed anything.

@fiyazbinhasan : Can you write a ReadMe.MD explaining how to build/use the formatters?

@tdykstra has first dibs as co-author, I'm 2nd in line. cc/ @spboyer

See Building Custom Formatters for .Net Core (Yaml Formatters)

@fiyazbinhasan
Copy link
Contributor

@Rick-Anderson readme.md file added :)

@tdykstra tdykstra self-assigned this Feb 2, 2017
@tdykstra
Copy link
Contributor

tdykstra commented Feb 2, 2017

Proposed outline for the doc

  • When to use:

    • There is a particular content type you want to enable that
      a. isn't supported [Edit: Out of Box, that is, in the product] OOB (i.e., not JSON, XML, or plain text) and
      b. You want to enable it in a way that integrates with content negotiation
    • Example use cases
      • Google Protobuf is a binary format that is more efficient than text types.
      • vCard is a text format commonly used for exchanging contact data. The sample app will implement a simple vCard formatter.
  • How to create a custom formatter class

    • For text media types (e.g., vcard, json, xml), derive from TextInputFormatter or TextOutputFormatter base class
      • Otherwise, derive from InputFormatter or OutputFormatter base class
    • Note that you can't do constructor DI in a formatter; to access services you need to use the context object that gets passed in to your methods.
    • In the constructor specify valid media types and encodings
      • Add to SupportedMediaTypes collection (e.g., application/vCard)
      • Add to SupportedEncodings collection
    • Override CanReadType/CanWriteType to specify the type you can deserialize into/serialize from (e.g., Contact type for vCard)
      • Note about output formatters: use CanWriteType for design time type, use CanWriteResult for runtime type
    • Override ReadRequestBodyAsync/WriteResponseBodyAsync to do the actual work of deserializing/serializing
  • How to configure MVC to use a custom formatter

    • Add the formatter to MvcOptions InputFormatters and/or OutputFormatters collections

@danroth27
Copy link
Member Author

@kichalla Please take a look at the proposed outline. Does the flow seem reasonable? Anything missing?

@kichalla
Copy link
Contributor

kichalla commented Feb 2, 2017

Looks good to me.

@tdykstra
Copy link
Contributor

Fixed by #2743

@tdykstra tdykstra removed this from the Backlog milestone Mar 14, 2017
trobinson41 pushed a commit to trobinson41/Docs that referenced this issue May 26, 2021
Mar 2 - tutorials, support tickets, subitem nav, various platform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants