Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 20, 2019 @ 10:17
    Koen van Ras-Neve
    0

    Using IScopeProvider inside IHttpHandler

    Hi, I'm using a Http Handler in my project to serve data as json on a certain URL, now I need to fetch some data from a custom database table with NPoco. I figured out I need to use the IScopeProvider to create a scope and use it with Database, though it looks like I can't use the scope provider in my http handler since it throws an error that I can't use a constructor.

    How can I use my database inside the http handler? Code below:

    class CustomJsonHandler : IHttpHandler
    {
        //private readonly IScopeProvider _scopeProvider;
    
        //public CustomJsonHandler(IScopeProvider scopeProvider)
        //{
        //  _scopeProvider = scopeProvider;
        //}
    
        public void ProcessRequest(HttpContext context)
        {
            JsonRoot file = new JsonRoot();
    
            // This doesn't work, but works in a controller I'm using somewhere else.
            //using (var scope = _scopeProvider.CreateScope())
            //{
            //   var result = scope.Database.Query<CustomJsonDto>().Where(x => x.NodeId == 1053).FirstOrDefault();
            //
            //  scope.Complete();
            //}
    
            // I want to fill this with database data.
            file.name = "Blabla";
            file.version = 2;
    
            string jsonOutput = JToken.Parse(JsonConvert.SerializeObject(file)).ToString(Formatting.Indented);
    
            context.Response.ContentType = "application/json";
            context.Response.Write(jsonOutput);
        }
    }
    
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 20, 2019 @ 10:54
    Sebastiaan Janssen
    100

    First off: can you achieve what you want to do with an APIController instead?

    What you're trying to do currently is more natively understood by apicontrollers, you don't need to do any json parsing, apicontrollers can return objects in perfectly nice json without having to use newtonsoft. You might need to put some attributes on your model to make it look good but other than that you're good to go.

    If you absolutely really HAVE to use an IHttpHandler then you can implement this dirty dirty code to do so, example:

    public class CustomJsonHandler : IHttpHandler
    {
        public bool IsReusable => false;
    
        public void ProcessRequest(HttpContext context)
        {
            using (var scope = Current.ScopeProvider.CreateScope())
            {
                var result = scope.Database.Fetch<string>("SELECT 'test'").FirstOrDefault();
    
                scope.Complete();
    
                context.Response.ContentType = "application/json";
                if(result != null)
                    context.Response.Write(result);
            }
        }
    }
    

    Try not to, ApiControllers are wonderful.. but if you MUST..

  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 20, 2019 @ 11:07
    Koen van Ras-Neve
    0

    The reason I use a http handler is that I want to generate for example a manifest file that is always available on

    <remove name="ManifestJsonHandler" />
    <add name="ManifestJsonHandler" type="Extensions.Handlers.HttpHandlers.ManifestJsonHandler" path="manifest.json" preCondition="integratedMode" verb="*" />
    

    If this is achievable with an API controller, please let me know how to :)

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 20, 2019 @ 12:02
    Sebastiaan Janssen
    0

    Is it required that it's /manifest.json? What is wrong with /umbraco/api/manifest/getmanifest? :-)

    You could also do a redirect from /manifest.json to the api endpoint if that makes it easier.

  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 20, 2019 @ 12:27
    Koen van Ras-Neve
    0

    That's a way I could do this, thanks! Also, should I avoid using http handlers entirely, or could I also use my http handler to grab the data from an API controller and then return it through the handler?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 20, 2019 @ 12:30
    Sebastiaan Janssen
    0

    Not sure, I would encourage you to google the pros and cons for httphandlers, as far as I recall they're only ever needed if you have very specific needs and in general you'd be just as well off using alternatives.

    But it really depends on the use case, hard to give general advise. :)

  • Koen van Ras-Neve 56 posts 361 karma points c-trib
    Mar 20, 2019 @ 13:39
    Koen van Ras-Neve
    0

    Will do, thanks!

Please Sign in or register to post replies

Write your reply to:

Draft