Office Add-in: get 404 when accessing an ApiController

79 Views Asked by At

I am learning to create a ppt add-in according to the official tutorial. I followed the steps in the Insert an Image section, but found the add-in did not work properly. When I clicked the "Insert Image" button, the add-in visited /api/Photo/ via ajax, but the state code was always 404.

This is the function called when I clicked the button:

function insertImage() {
    // Get image from from web service (as a Base64 encoded string).
    $.ajax({
        url: "/api/Photo/", success: function (result) {
            insertImageFromBase64String(result);
        }, error: function (xhr, status, error) { // always 404
            showNotification("Error", "Oops, something went wrong.");
        }
    });
}

And this is the ApiController:

namespace HelloWorldWeb.Controllers
{
    public class PhotoController : ApiController
    {
        public string Get()
        {
            string url = "http://www.bing.com/HPImageArchive.aspx?format=xml&idx=0&n=1";

            // Create the request.
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();

            using (Stream responseStream = response.GetResponseStream())
            {
                // Process the result.
                StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);
                string result = reader.ReadToEnd();

                // Parse the xml response and to get the URL.
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(result);
                string photoURL = "http://bing.com" + doc.SelectSingleNode("/images/image/url").InnerText;

                // Fetch the photo and return it as a Base64 encoded string.
                return getPhotoFromURL(photoURL);
            }
        }

        private string getPhotoFromURL(string imageURL)
        {
            var webClient = new WebClient();
            byte[] imageBytes = webClient.DownloadData(imageURL);
            return Convert.ToBase64String(imageBytes);
        }
    }
}

I wonder how can I config the routers. It has not been discussed in the tutorial and I am confused whether it is configured automatically.

Thank you!

0

There are 0 best solutions below