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);
}
}
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..
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?
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. :)
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:
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:Try not to, ApiControllers are wonderful.. but if you MUST..
The reason I use a http handler is that I want to generate for example a manifest file that is always available on
If this is achievable with an API controller, please let me know how to :)
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.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?
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. :)
Will do, thanks!
is working on a reply...