办公室加载项:访问apicontroller时获取404

发布于 2025-01-25 10:53:25 字数 2313 浏览 2 评论 0原文

我正在学习根据官方教程。我遵循 插入图像,但发现加载项无法正常工作。当我单击“插入映像”按钮时,访问的加载项/api/photo/通过ajax,但状态代码始终为404。

这是我单击按钮时调用的函数:

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.");
        }
    });
}

这​​是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 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文