Is there Web API "HelloWorld" plugin for Umbraco CMS?
Hi Everyone,
I would like to ask a tutorial to create a sample Web API plugin for Umbraco CMS. It is just like a plugin that can call Web Api controller to print out a Hello World.
using System.Web.Http;
using Umbraco.Web.WebApi;
public class HelloWorldApiController : UmbracoApiController
{
// /Umbraco/Api/HelloWorldApi/Print
[HttpGet]
public string Print()
{
return "Hello World";
}
}
I have seen the document but it doesn't show how to start with. Maybe I need to start create a new project along with Umbraco Site or start to create an installable package and add some cs files to existing Umbraco Site. Or something else...?
Actually, all the guild for how to use Umbraco Api is here. But the key is to create a controller in folder App_Code of deployed version of Umbraco CMS; the controller then must be inherited from UmbracoApiController.
There is an example:
public class ProductsController : UmbracoApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
public IEnumerable<Product> GetAllProducts()
{
//Connect to the Umbraco DB
var db = ApplicationContext.DatabaseContext.Database;
//Get an IENumberable of Product objects to iterate over
var products = db.Fetch<Product>(new Sql().Select("*").From("Products"));
return products;
}
public IHttpActionResult GetProduct(int id)
{
//Connect to the Umbraco DB
var db = ApplicationContext.DatabaseContext.Database;
//Get an IENumberable of Products objects to iterate over
var product = db.Fetch<Product>("SELECT * FROM Products WHERE Id=@0", id).FirstOrDefault();
if (product == null)
{
return NotFound();
}
return Ok(product);
}
}
All api url from Umbraco should start with “api” and then Controller Name “Products” and then Method Name “GetAllProducts”
Example: “api/Products/GetAllProducts”
Example Ajax:
var uri = 'api/Products/GetAllProducts';
$(document).ready(function () {
// Send an AJAX request
$.getJSON(uri)
.done(function (data) {
// On success, 'data' contains a list of products.
$.each(data, function (key, item) {
// Add a list item for the product.
$('<li>', { text: formatItem(item) }).appendTo($('#products'));
});
});
});
Note: For how to create a helloworld plugin for Umbraco can be seen here.
Is there Web API "HelloWorld" plugin for Umbraco CMS?
Hi Everyone,
I would like to ask a tutorial to create a sample Web API plugin for Umbraco CMS. It is just like a plugin that can call Web Api controller to print out a Hello World.
Sincerely,
Tri
Not just this?
I also assume you've seen the documentation?
I have seen the document but it doesn't show how to start with. Maybe I need to start create a new project along with Umbraco Site or start to create an installable package and add some cs files to existing Umbraco Site. Or something else...?
Yes. I think that's best.
Actually, all the guild for how to use Umbraco Api is here. But the key is to create a controller in folder App_Code of deployed version of Umbraco CMS; the controller then must be inherited from UmbracoApiController.
There is an example:
All api url from Umbraco should start with “api” and then Controller Name “Products” and then Method Name “GetAllProducts” Example: “api/Products/GetAllProducts” Example Ajax:
Note: For how to create a helloworld plugin for Umbraco can be seen here.
is working on a reply...