asp.net web api - In a WebAPI Formatter, how can you get the URL from HttpContent? -
in webapi service, using formatter read content parameter on request. need access url in order transform content parameter correctly. httprequestmessage isn't available, , can't utilize httpcontext.current.request because httpcontext.current null. accessing httprequestmessage on read requested @ http://aspnetwebstack.codeplex.com/workitem/82, issue closed because httpcontent available on read. however, don't know how url httpcontent, or if it's possible.
there method called getperrequestformatterinstance on formatter can override create new instance of formatter stateful info request in it. way, method getperrequestformatterinstance called during request's deserialization stage. illustration below:
public class textplainformatter : bufferedmediatypeformatter { public textplainformatter() { this.supportedmediatypes.add(new mediatypeheadervalue("text/plain")); } public httprequestmessage currentrequest { get; private set; } public override mediatypeformatter getperrequestformatterinstance(type type, httprequestmessage request, mediatypeheadervalue mediatype) { textplainformatter frmtr = new textplainformatter(); frmtr.currentrequest = request; //copy original formatter instance new instance frmtr.mediatypemappings.clear(); foreach (mediatypemapping mediatypemapping in this.mediatypemappings) { frmtr.mediatypemappings.add(mediatypemapping); } frmtr.requiredmemberselector = this.requiredmemberselector; frmtr.supportedencodings.clear(); foreach (encoding supportedencoding in this.supportedencodings) { frmtr.supportedencodings.add(supportedencoding); } frmtr.supportedmediatypes.clear(); foreach (mediatypeheadervalue supportedmediatype in this.supportedmediatypes) { frmtr.supportedmediatypes.add(supportedmediatype); } homecoming frmtr; } asp.net-web-api
No comments:
Post a Comment