Copied to clipboard

Flag this post as spam?

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


  • JoDev 1 post 71 karma points
    Feb 23, 2016 @ 09:40
    JoDev
    0

    Want a web service to output data on a webpage - how?

    Hi,

    I'm new to Umbraco and I don't understand how to achieve this. My main goal is to use a web service (.asmx) getting data from a database and list it on an Umbraco webpage for the user to see.

    As a first step I'd just like the web service/Umbraco to output a Hello world text on the webpage for me to visually see that it works. Database and stuff will be my next task.

    How can I achieve this? I followed a guide but couldn't find the usercontrol folder in Visual Studio due to me using MVC/Razor (not Webforms), so that failed and now I'm clueless. I also saw that Umbraco Forms has some kind of web service - is this an alternative?

    Please help, thank you!

  • Ryan 34 posts 138 karma points
    Feb 24, 2016 @ 12:18
    Ryan
    0

    This is a very simple solution and should give you a very basic starting point. I think it may be worth while checking out some tutorials on Web API.

    //Create web api method to return hello world
    public class TestApiController : UmbracoApiController
    {
        [HttpGet]
        public string Get()
        {
            return "Hello World";
        }
    }
    //Create method to call web api function, this method can be placed   
    //inside a controller e.g. inside you home controller
    
    public class HomeController : Umbraco.Web.Mvc.RenderMvcController
    {
       public override ActionResult Index(RenderModel model)
       {
        //call web api method 
        var text = GetHelloWorldText();
    
        return base.Index(model);
       }
    }
    
    public string GetHelloWorldText()
    {
        using (var client = new HttpClient())
        {
           var result =
               client.GetStringAsync("http://url/umbraco/api/testapi/get")
               .Result;
    
                return result;
        }
     }
    

    You can then pass the text into the view via a model, TempData or ViewBag. You could also call the web api method from within your view using Javascript.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies