Umbraco SurfaceController-无法获取CurrentPage

时间:2018-09-07 10:18:56

标签: c# model-view-controller umbraco

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

}

1 个答案:

答案 0 :(得分:0)

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

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

您可以尝试以下代码:

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