使用ASP为office插件添加web服务.. NET web api

Adding a web service for office add-in using ASP.NET web api

本文关键字:web 服务 NET api 添加 插件 ASP office 使用      更新时间:2023-09-26

我按照这篇文章为我的office 365插件添加了web服务。但是当我执行发送请求的ajax脚本时,它总是以。fail语句结束。我看不到程序执行,甚至在控制器类或全局的单行。

这是我的javascript代码片段

function GetStyles(ooxml) {
    var data = {
        xml: ooxml
    };
    $.ajax({
        url: 'api/GetStyles',
        type: 'GET',
        data: data,
        contentType: 'application/json;charset=utf-8'
    }).done(function (data) {
        showNotification(data.Status, data.Message);
    }).fail(function (status) {
        showNotification('Error', 'Could not communicate with the server.');
    }).always(function () {
        // $('.disable-while-sending').prop('disabled', false);
    });
}

这是控制器类

public class WriterController : ApiController
{
    public class FeedbackRequest
    {
        public string xml;
    }
    public class FeedBackResponse
    {
        public string status;
        public string message;
    }
    [HttpGet()]
    public FeedBackResponse GetStyles(FeedbackRequest request)
    {
        try
        {
            return new FeedBackResponse
            {
                status = "success",
                message = "success success",
            };
        }
        catch (Exception e)
        {
            return new FeedBackResponse
            {
                status = "error",
                message = "error error",
            };
        }
    }
}
有人能帮我解决这个问题吗?

只看代码,没有什么明显的错误。但是我可以给你一些关于如何调试它的建议。

首先,尝试在Office外接程序的上下文中调试它。它没有任何"外接程序"。因此,只需在Internet Explorer中启动html页面,或者更好的是,使用Fiddler发出请求。这个结果应该能让我们清楚地知道外接程序出了什么问题。