How can i implement web API in Orchard custom module

Topics: Customizing Orchard
Aug 17, 2015 at 4:48 AM
Hi
I have one web API URL and it's returns some XML values, how can I retrieve that XML values into my custom module.

API URL like: www.website.com/api/GetValue?value1=23&value2=newvalue&searchItem=products
Aug 17, 2015 at 8:10 AM
Edited Aug 17, 2015 at 8:12 AM
You can just create a module and throw in an API controller in there with those methods. Then you only need to define the Routes to get it working with the url you want.
The default route that is made by Orchard to your api is as following:

/api/{module.name}/{controller}/{id}

To cleanly get the url in your code, you can use the following piece:

var actionUrl = Url.Action("Method", "Controller", new {
    httproute = true,
    area = "MyModuleName"
});
Aug 17, 2015 at 1:44 PM
Edited Aug 17, 2015 at 1:48 PM
Can you please show one sample code?
Aug 17, 2015 at 3:58 PM
Hi Nitheesh - I've just been playing about with this :)

Here's a snippet based on what I did - this would bring data from a hypothetical materials part, using a materials viewmodel, you'd get JSON back
        
        public IEnumerable<MaterialViewModel> Get()
        {
            List<MaterialViewModel> m = new List<MaterialViewModel>();
            var x = _materialRepository.Table;
            foreach (var item in x)
            {
                m.Add(new MaterialViewModel
                {
                    Title = item.Title,
                    MaterialID = item.MaterialID,
                    Description = item.Description
                });
            }

            return m.AsEnumerable();
        }
hope that helps
Aug 17, 2015 at 4:44 PM
Thank you eRuth....