Umbraco SurfaceController - 无法获取CurrentPage

问题描述 投票:0回答:1

我有一个执行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
       ...
    }
}

}

c# model-view-controller umbraco
1个回答
0
投票

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

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

你可以试试下面的代码:

UmbracoHelper helper = new UmbracoHelper(UmbracoContext.Current);
IPublishedContent content = helper.TypedContent(Node.getCurrentNodeId());
© www.soinside.com 2019 - 2024. All rights reserved.