Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AspnetCore RazorPage return raw string?

RazorPage typically return void, Task, IActionResult, such as

public Task OnGet() { }
public Task<IActionResult> OnGet() { }

It's also possible having a handler returning Json, like

public Task<JsonResult> OnGetAlso() { }

But there are scenarios where I want the handler to return raw string, but I couldn't find an easy way to do this.

public Task<string> OnGetSomeString() 
{ return "something"; }

But I always get errors Unsupported handler method return type. Is this possible? Thanks

like image 799
Whoever Avatar asked Apr 25 '19 16:04

Whoever


1 Answers

Use a ContentResult to return string content:

public IActionResult OnGetSomeString()
{
    return Content("something");
}
like image 169
Versailles Avatar answered Oct 09 '22 14:10

Versailles