简体   繁体   中英

Expose partial razor view into web api controller

I'm trying to use elements of the boilerplate Login view in a React app.

In standard ASP.NET Core 2 Identity projects, we have

@await Html.PartialAsync("_LoginPartial")

in _Layout.html . I want to expose this content in a controller, to get the content in a React app.

The idea would be to have in the controller something like this:

public async Task<IActionResult> LoginPartialAsync()
{
    return Content(await Html.PartialAsync("_LoginPartial"));
}

However, I cannot get to know where the Html comes from (sounds to be a IHtml<dynamic> , it's not Microsoft.AspNetCore.Html ).

Any idea ?

You don't need the HtmlHelper , you can use the PartialView method:

public IActionResult LoginPartial()
{
    return PartialView("_LoginPartial");
}

In the case the ASP.NET project doesn't have the directory Views but Pages which contains _LoginPartial.chtml , we need to provide:

    public IActionResult LoginPartial()
    {
        return PartialView("../../Pages/_LoginPartial");
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM