gpt4 book ai didi

umbraco - 如何在 UmbracoApiController 中创建继承 RenderModel 的模型实例?

转载 作者:行者123 更新时间:2023-12-02 20:48:14 27 4
gpt4 key购买 nike

我正在使用 Umbraco CMS。我有以下模型:

 public class LoyaltyPromo : RenderModel
{
public LoyaltyPromo(IPublishedContent content)
: base(content)
{
}
//properties removed for brevity
}

我想在 UmbracoApiController 中使用这个模型。所以我这样做:

    public class PromoServiceController : UmbracoApiController
{
public async Task<object> GetAll()
{

var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent content = umbracoHelper.TypedContent(1050);
var list = new List<LoyaltyPromo>();
list.Add(new LoyaltyPromo(content));
return list;

}
}

不幸的是它不起作用,我得到了NullReferenceException:

Screenshot where the exception occurs

An error has occurred. Object reference not set to an instance of an object. System.NullReferenceException at Umbraco.Web.Models.RenderModel..ctor(IPublishedContent content) at LoyaltyOps.Models.LoyaltyPromo..ctor(IPublishedContent content) in C:\Users\mkallingal\documents\visual studio 2015\Projects\LoyaltyOps\LoyaltyOps.Models\LoyaltyPromo.cs:line 13 at LoyaltyOps.Controllers.PromoServiceController.d__0.MoveNext() in C:\Users\mkallingal\documents\visual studio 2015\Projects\LoyaltyOps\LoyaltyOps\Controllers\PromoServiceController.cs:line 32 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.TaskHelpersExtensions.d__3`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()

我该如何解决这个问题?

最佳答案

您的问题是 RenderModel 不适合在 WebAPI 中使用,它根本没有关于前端页面的上下文。在内部,RenderModel 依赖于正确设置的 UmbracoContextPublishedContentRequest 属性,而 WebAPI 不会发生这种情况。

如果您确实需要将模型建立在 RenderModel 的基础上,请使用替代构造函数:

RenderModel(IPublishedContent 内容,CultureInfo 文化)

并传入文化和内容 - 这将绕过设置 PublishedContentRequest 的需要,但您可能会进一步遇到其他问题。

更好的方法是创建一个完全不依赖于 RenderModel 的 View 模型。由于 WebAPI 通常会将模型序列化为 JSON,因此模型越轻越好。

另一件事 - 您的 GetAll() 列表根本不返回 - 我假设您的实际方法中没有进行额外的处理,需要您的模型基于 RenderModel ,并且您只是返回列表?

关于umbraco - 如何在 UmbracoApiController 中创建继承 RenderModel 的模型实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37430981/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com