asp.net-web-api - 我无法使用 umbraco 项目在 WebAPI 中使用 Get 方法
问题描述
我很难打 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
call 并返回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 项目。
这是错误堆栈跟踪-在我看来,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.<Init>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& completedSynchronously) +71
编辑
我将路由配置从更改api
为dataapi
例如。routeTemplate:"dataapi/{controller}/{id}"
所以它现在运行但仍然抛出错误:
405 Method not allowed
编辑 2
知道了!将方法名称从更改为ProductList
,Get
现在可以使用
解决方案
您可以改用 UmbracoApiController 来确保 Umbraco 不会尝试将 URL 与内容匹配?然后控制器的路径将改为 /umbraco/api/yourcontroller,但一切都应该工作相同。查看https://our.umbraco.com/documentation/reference/routing/webapi/了解详情。
推荐阅读
- c# - 在 NAudio 中使用断言
- javascript - 如何为一个元素设置更改属性?使用 JS 而不是 JQuery
- selenium - “org.openqa.selenium.WebDriverException:java.net.SocketException:软件导致连接中止:recv 失败”尝试启动 Firefox
- java - 流 - 过滤所有不抛出异常
- maven - 部署到 Heroku 时自定义 Maven 命令
- python - 将生成的 csv 文件分配给 django 模型
- python - .split() 是否始终保留字符串中元素的顺序?
- ios - SDWebImage 正在部分加载,即使在终止应用程序后也没有加载回来
- matplotlib - 在 imshow 中绘图未正确采用 x 值
- webpack - WEBPACK 4 SplitChunksPlugin 不拆分