Access BaseUri in MediaTypeFormatter?

Topics: Web Api
Oct 4, 2011 at 7:53 PM

I´d like to modify the serialization behaviour of my custom media type formatter. respectively, i want to omit the domain of the base uri of the request in all links of my representation - transforming them into relative URI´s according to the requests context. And on deserialzing the other way around.

How can i access the BaseUri in a MediaTypeFormatter (OnReadFromStream and OnWriteToStream)? Or is there another way to archieve that? The only way i can think of, is ctor injection of the uri... But i´d like to use an automatic procedure...

Thanks in advance...

Coordinator
Oct 5, 2011 at 5:34 AM

One way to achieve this is to add an operation handler that grabs the request URI from the request and adds it to the content headers collection so that it can then later be picked up by your formatter. You will then also need a message handler to remove the custom header so that it doesn’t go out on the wire.

To provide a better experience we are also looking at providing access to the request/response from within the formatters. If this is important to you please file an issue in the Issue Tracker so that it can be voted on by the community.

Thanks!

Daniel Roth

From: humbrie [email removed]
Sent: Tuesday, October 04, 2011 12:54 PM
To: Daniel Roth
Subject: Access BaseUri in MediaTypeFormatter? [wcf:274758]

From: humbrie

I´d like to modify the serialization behaviour of my custom media type formatter. respectively, i want to omit the domain of the base uri of the request in all links of my representation - transforming them into relative URI´s according to the requests context. And on deserialzing the other way around.

How can i access the BaseUri in a MediaTypeFormatter (OnReadFromStream and OnWriteToStream)? Or is there another way to archieve that? The only way i can think of, is ctor injection of the uri... But i´d like to use an automatic procedure...

Thanks in advance...