【问题标题】:Umbraco SurfaceController - cannot get CurrentPageUmbraco SurfaceController - 无法获取 CurrentPage
【发布时间】:2018-09-17 14:57:47
【问题描述】:

我有一个执行 Get 方法的 SurfaceController。当我尝试使用 this.CurrentPage 访问当前页面时,出现以下错误

在路由值中找不到 Umbraco 路由定义, 请求必须在 Umbraco 请求的上下文中提出

为什么我的控制器看不到当前页面?

{ 使用系统; 使用 System.Threading.Tasks; 使用 System.Web.Mvc; ... 使用 Umbraco.Web.Mvc;

public class BenefitStatementPdfSurfaceController : SurfaceController
{
    private readonly SelfServiceApiHttpClient _apiClient;

    public MySurfaceController(SelfServiceApiHttpClient apiClient)
    {
        _apiClient = apiClient;
    }

    [ActionName("MemberPdf")]
    [HttpGet]
    [Authorize]
    public async Task<ActionResult> MemberPdfAsync(int memberNumber, int selectedYear)
    {
        var content = await _apiClient
                                .MemberPdfAsync(memberNumber, selectedYear)
                                .ConfigureAwait(false);
        if (content.HasPdf)
        {
                return this.File(content.Pdf, "application/pdf", $"Statement{selectedYear}_{DateTime.Now:yyyyMMdd}_{DateTime.Now:HHmmss}.pdf");
        }

        var nodeId = this.CurrentPage.Id; ------ERRORS HERE
       ...
    }
}

}

【问题讨论】:

  • 删除 .ConfigureAwait(false) 有什么改变吗?
  • 不,还是一样的错误
  • 嘿伙计,如果我在表面控制器中是正确的,那么默认情况下你不在 umbracocontext 中,但表面控制器提供了一个 Umbraco 助手。我最近做了类似的事情,我所做的是传递页面的 id(这是我的 ajax 调用),然后使用 Umbraco.TypedContent(pageId)。这将为您提供当前页面,您可以从那里访问当前页面属性。在这种情况下,“Umbraco”是 Umbraco 助手。那有帮助吗?附言我认为您也可以使用“AssignedContentItem”来获取页面的属性,除非它是嵌套内容。

标签: c# model-view-controller umbraco


【解决方案1】:

我认为您需要使用当前的 UmbracoContext。

Umbraco 有一些你可以使用的辅助方法。

你能不能试试下面的代码:

UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent content = helper.TypedContent(Node.getCurrentNodeId());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多