I have ASP.NET 5 Web Api and need to do the following task:

Add endpoint, which get cshtml page and return generated html, so it should do the same what is does razor view engine.

What is better way to do this?

I added these views in this web api project and I think I can just make httpget request to view to get html response. Is it correct way?

So actually I need to covert ViewResult to ContentResult.

  • havent tested but surely you just add an mvc controller and return a view as normal?
    – Ewan
    Commented Jul 18, 2021 at 17:38
  • @Ewan, I don't need to show view, just return html as string.
    – Alexan
    Commented Jul 18, 2021 at 17:42
  • 2
    thats what webpages do
    – Ewan
    Commented Jul 18, 2021 at 17:48
  • @Ewan, maybe you're right. Than I just need to replace services.AddControllers() to services.AddControllersWithViews() and create endpoint that returns this view.
    – Alexan
    Commented Jul 18, 2021 at 17:59
  • 1
    if you want to return the string as an escaped json object then you can call the razor engine in code codemag.com/article/1312081/…
    – Ewan
    Commented Jul 18, 2021 at 18:11


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.