我无法使用umbraco项目在WebAPI中点击Get方法

时间:2018-08-10 11:06:17

标签: asp.net-web-api asp.net-web-api2 umbraco umbraco7 asp.net-web-api-routing

我无法执行webapi操作。 我在控制台中收到500错误,但未达到api。

这是我的ajax调用:

function getProducts() {
    var response;
    $.getJSON("/api/Product")
        .done(function (data) {
            response = $.map(data,
                function (item) {
                    return { label: item.Name + ' (' + item.Code + ')' };
                });
        });
    return response;
};

ajax没有被击中。该请求跳过getJson调用并返回undefined response

这是我的api控制器方法:

public class ProductController : ApiController {

    // GET api/<controller>
        public IEnumerable<ProductModel> ProductList()
        {
            ProductSearcher searcher = new ProductSearcher();
            return searcher.GetResults();
        }
    }

在配置中,我定义了:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

邮递员返回Object reference not set to an instance of an object,但也没有碰到ProductList

我已选择运行多个项目-我的应用程序启动项目和Web api项目。

这是错误stacktrace-在我看来,umbraco确实在寻找路由,而我需要路由到非umbraco api:

[NullReferenceException: Object reference not set to an instance of an object.]
   Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.HandlePageNotFound(PublishedContentRequest docRequest) +152
   Umbraco.Web.Routing.ContentLastChanceFinderByNotFoundHandlers.TryFindContent(PublishedContentRequest docRequest) +10
   Umbraco.Web.Routing.PublishedContentRequestEngine.HandlePublishedContent() +529
   Umbraco.Web.Routing.PublishedContentRequestEngine.FindPublishedContentAndTemplate() +250
   Umbraco.Web.Routing.PublishedContentRequestEngine.PrepareRequest() +107
   Umbraco.Web.UmbracoModule.ProcessRequest(HttpContextBase httpContext) +361
   Umbraco.Web.UmbracoModule.&lt;Init&gt;b__8(Object sender, EventArgs e) +80
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +141
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +71

编辑

例如,我将路由配置从api更改为dataapi。 routeTemplate:"dataapi/{controller}/{id}",因此它现在可以运行,但仍然会引发错误:

405 Method not allowed

编辑2

知道了!将方法名称从ProductList更改为Get,现在可以使用

1 个答案:

答案 0 :(得分:0)

您可以改用UmbracoApiController来确保Umbraco不尝试将URL与内容匹配?然后,到控制器的路径将是/ umbraco / api / yourcontroller,但所有操作都应相同。看看https://our.umbraco.com/documentation/reference/routing/webapi/了解详情。