办公室加载项:访问apicontroller时获取404
我正在学习根据官方教程。我遵循 插入图像节,但发现加载项无法正常工作。当我单击“插入映像”按钮时,访问的加载项/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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论