How do you access Current HttpContext in a MediaTypeFormatter? It is working if I inherit from the BufferedMediaTypeFormatter, but if I inherit the MediaTypeFormatter HttpContext.Current is null. Is there a way around that?
It is recommened that you don't access HttpContext within WebApi. If you are trying to store data that should persist over the entirety of a request, then use the HttpRequestMessage properties collection.
What type of data are you trying to obtain from the context?
I am just playing around preparing a demo on web api and wanted an easy way to store files and control of where I stored them within my application. So I was trying to do a Server.MapPath operation basically. Is there a new recommended way to do that?
I don't see a straight forward way of getting at this data. Off the top of my head, there are a lot of different ways to solve this within Global.asax. One way is to access the request and get the relevant path info from the global.asax begin request handler
and then store it in the HttpConfiguration properties bag (GlobalConfiguration.Configuration.Properties). Later in the pipeline, you can open up the configuration properties and retrieve this value.
While this is not meant to be exposed, you can access the http context via request.Properties["MS_HttpContext"], I wouldn't use this in production but for demo purposes it might work for you.
wanted an easy way to store files and control of where I stored them within my application. So I was trying to do a Server.MapPath operation basically
For media type formatters, especially those that write types, (for me) it's all about data preparation. I.e. the resource (or model) type that is written out by your media type formatter should be pre-prepared with all the data it needs earlier on in the
processing pipeline. So you could, for example, do the Server.MapPath in your controller, and store the resulting path in the resource/model type that the media type formatter will operate on.
Member
1 Points
17 Posts
Access to HttpContext.Current in a async MediaTypeFormatter
Mar 19, 2012 07:05 PM|mastoj|LINK
How do you access Current HttpContext in a MediaTypeFormatter? It is working if I inherit from the BufferedMediaTypeFormatter, but if I inherit the MediaTypeFormatter HttpContext.Current is null. Is there a way around that?
--Tomas
Member
131 Points
93 Posts
Re: Access to HttpContext.Current in a async MediaTypeFormatter
Mar 19, 2012 07:12 PM|davebettin|LINK
It is recommened that you don't access HttpContext within WebApi. If you are trying to store data that should persist over the entirety of a request, then use the HttpRequestMessage properties collection.
What type of data are you trying to obtain from the context?
@dbettin
Member
1 Points
17 Posts
Re: Access to HttpContext.Current in a async MediaTypeFormatter
Mar 19, 2012 07:21 PM|mastoj|LINK
I am just playing around preparing a demo on web api and wanted an easy way to store files and control of where I stored them within my application. So I was trying to do a Server.MapPath operation basically. Is there a new recommended way to do that?
Member
131 Points
93 Posts
Re: Access to HttpContext.Current in a async MediaTypeFormatter
Mar 19, 2012 07:57 PM|davebettin|LINK
I don't see a straight forward way of getting at this data. Off the top of my head, there are a lot of different ways to solve this within Global.asax. One way is to access the request and get the relevant path info from the global.asax begin request handler and then store it in the HttpConfiguration properties bag (GlobalConfiguration.Configuration.Properties). Later in the pipeline, you can open up the configuration properties and retrieve this value.
While this is not meant to be exposed, you can access the http context via request.Properties["MS_HttpContext"], I wouldn't use this in production but for demo purposes it might work for you.
@dbettin
Member
81 Points
90 Posts
Re: Access to HttpContext.Current in a async MediaTypeFormatter
Mar 20, 2012 03:12 AM|awebb|LINK
For media type formatters, especially those that write types, (for me) it's all about data preparation. I.e. the resource (or model) type that is written out by your media type formatter should be pre-prepared with all the data it needs earlier on in the processing pipeline. So you could, for example, do the Server.MapPath in your controller, and store the resulting path in the resource/model type that the media type formatter will operate on.
Member
1 Points
17 Posts
Re: Access to HttpContext.Current in a async MediaTypeFormatter
Mar 20, 2012 04:14 AM|mastoj|LINK
Thank you for your answer, it is definitely an option.