UmbracoAuthorizedJsonController returning empty result
I have a controller with a GetById function
public Product GetById(int id ) {
var query = new Sql().Select("*").From("jf_product").Where<Product>(x => x.Id == id); var p = DatabaseContext.Database.Fetch<Product>(query).FirstOrDefault();
I also can hit a debug marker in GetById and can see that variable p is populated and returned. Yet I get a faulty result in response. The result from the request is just:
)]}', {}
I found a tip that I could test to use the UmbracoAuthorizedApiController instead, but then the result is just {}
Somewhere the Json serializer don't work as expected. The Product object returned is a poco object.
[TableName("jf_product")] [DataContract(Name = "product")] public class Product { public Product() { }
[PrimaryKeyColumn(AutoIncrement = true)] public int Id { get; set; }
public string ItemNo { get; set; } public string Name { get; set; } public string UnitOfMeasure { get; set; } public bool IsInStock { get; set; } public int SortOrder { get; set; } public bool SoldOutWeb { get; set; } }
Just a wild guess. But I had a problem once with a property called name. Maybe giving it another name for json using the datacontract attribute will work.
If you use UmbracoAuthorizeApiController you need to add [Umbraco.Web.WebApi.UmbracoAuthorize] before your method.
Like this:
[Umbraco.Web.WebApi.UmbracoAuthorize]
public Product GetById(int id )
{
var query = new Sql().Select("*").From("jf_product").Where<Product>(x => x.Id == id);
var p = DatabaseContext.Database.Fetch<Product>(query).FirstOrDefault();
return p;
}
UmbracoAuthorizedJsonController returning empty result
I have a controller with a GetById function
public Product GetById(int id )
{
var query = new Sql().Select("*").From("jf_product").Where<Product>(x => x.Id == id);
var p = DatabaseContext.Database.Fetch<Product>(query).FirstOrDefault();
return p;
}
From the backend I call this and the url is correct http://localhost:56682/umbraco/backoffice/JfpAddon/ProductApi/GetById?id=13
I also can hit a debug marker in GetById and can see that variable p is populated and returned. Yet I get a faulty result in response. The result from the request is just:
)]}',
{}
I found a tip that I could test to use the UmbracoAuthorizedApiController instead, but then the result is just {}
Somewhere the Json serializer don't work as expected. The Product object returned is a poco object.
[TableName("jf_product")]
[DataContract(Name = "product")]
public class Product
{
public Product() { }
[PrimaryKeyColumn(AutoIncrement = true)]
public int Id { get; set; }
public string ItemNo { get; set; }
public string Name { get; set; }
public string UnitOfMeasure { get; set; }
public bool IsInStock { get; set; }
public int SortOrder { get; set; }
public bool SoldOutWeb { get; set; }
}
Hi Jan
How many results did you expect to get returned? Could there be a query done against the wrong table for instance?
/Jan
Sorry for late reply, but I expected umbraco to convert my Product class to json format. I have a debug breakpoint and see that var p is the correct post, but the result when using firebug says that response from http://localhost:56682/umbraco/backoffice/JfpAddon/ProductApi/GetById?id=21 is just
Just a wild guess. But I had a problem once with a property called name. Maybe giving it another name for json using the datacontract attribute will work.
But like i said it is a wild guess.
Dave
I'm getting those )]}', characters at the beginning of my response too, any idea what that is?
Hey,
If you use UmbracoAuthorizeApiController you need to add [Umbraco.Web.WebApi.UmbracoAuthorize] before your method.
Like this:
is working on a reply...