41

I have a method which creates an HttpResponseMessage containing an Error object which will be returned based on the current request media type formatter.

Currently, I have hardcoded the XmlMediaTypeFormatter but I'd like to be able to find the current request MediaTypeFormatter at runtime but I don't have access to the current request object since my below code exists on a separate class library.

private HttpResponseMessage Create(HttpStatusCode statusCode, string errorCode, string errorMessage)
{
    var result = new HttpResponseMessage(statusCode)
        {
            Content = new ObjectContent<Error>(new Error()
            {
                Code = errorCode,
                Message = errorMessage
            }, new XmlMediaTypeFormatter())
        };
    return result;
}

How to access the current HttpRequestMessage object globally? something like HttpContext.Current.Request

If impossible, how to implement the above method so that it knows which formatter it should be using for the current request?

tereško
  • 58,060
  • 25
  • 98
  • 150
The Light
  • 26,341
  • 62
  • 176
  • 258
  • 1
    Why don't you pass a `HttpRequestMessage` to your `XmlMediaTypeFormatter`? if you say that your formater is in another library it would be better not to strongly link it to your application. – polkduran May 21 '13 at 13:54
  • thanks, I'm not using any custom formatter. I was hoping I can avoid passing the Controller Request property. – The Light May 21 '13 at 15:07
  • I finally passed the Request, as there was no better solution. – The Light May 22 '13 at 15:57

3 Answers3

82

It's not impossible as I have just recently found out. It's actually added into the Items property of the current HttpContext (if there is one) =[

HttpRequestMessage httpRequestMessage = HttpContext.Current.Items["MS_HttpRequestMessage"] as HttpRequestMessage

Edit:

This is as of WebAPI v2 .. I cannot be sure of previous versions.

dariusc
  • 1,069
  • 9
  • 19
  • 4
    It's not in WebAPI v1. – SeriousM Nov 29 '13 at 12:24
  • @Magrangs if you are using WebAPI 2 then yes it's ok to use. The ASP pipleline stores the message object on the current httpcontext. You can debug into a session yourself and inspect the various keys in `HttpContext.Current.Items`. Just note that this could change in future versions of the WebAPI – dariusc Oct 14 '14 at 13:59
  • 6
    Note also that this is unlikely to work in a self-hosted environment (e.g. if you are using OWIN or similar), as HttpContext.Current won't be set. – RB. Jul 07 '15 at 13:56
  • 1
    @RB. how would you acccomplish this in a self-hosted env? – Alex Gordon Jul 27 '17 at 19:47
  • @MehdiDehghani 4 years later and on a different platform it doesn't work? Please do tell – dariusc Aug 07 '17 at 06:06
  • @dariusc As `DNN 7.x` use `WebApi` [Ref](http://www.dnnsoftware.com/community-blog/cid/142400/getting-started-with-services-framework-webapi-edition), in my project I use `WebApi` as well, I need to access to `HttpRequestMessage object globally`, I used your solution and it doesn't work, I wrote a comment here to help other people that may have same question as me. – Mehdi Dehghani Aug 07 '17 at 06:19
1

Why not do what the Web API team have done with their CreateResponse method? Make it an extension method of Controller. That way you can still have the code in a separate class library, but your method will have access to the controller instance and therefore all the Configuration information.

And on a slightly different note, I would suggest you look into some of the standardization efforts for error responses rather than inventing your own.

e.g.:

Community
  • 1
  • 1
Darrel Miller
  • 139,164
  • 32
  • 194
  • 243
  • 2
    hmm, How to get the Controller instance then at runtime from a separate class? don't think your suggestion can be used. – The Light May 22 '13 at 08:21
  • Hi, thanks for this post. Can you offer an update at this distance as to whether either has gained traction and/or whether it's been supplanted as an idea (or by something else)? – Ruben Bartelink Jan 14 '17 at 15:52
1

You can try to archieve it with Autofac, eg

public class MyPrincipal : IPrincipal
    {
        private readonly IPrincipal principal_;

        public MyPrincipal(ILifetimeScope scope)
        {
            if (scope.Tag == null || scope.Tag != MatchingScopeLifetimeTags.RequestLifetimeScopeTag)
            {
                throw new Exception("Invalid scope");
            }

            principal_ = scope.Resolve<HttpRequestMessage>().GetRequestContext().Principal;
        }
}

This class can be registred with InstancePerRequest lifetime.

   builder.RegisterType<MyPrincipal>().As<IPrincipal>().InstancePerRequest();
Lonli-Lokli
  • 3,357
  • 1
  • 24
  • 40