0

I have API that based on ASP.NET Core 2.2 and I want to return result not from my public method that handles request but from an inner method.

public class UsersController : MainController
        {
            [HttpGet("{id:int}")]
            public IActionResult Get(int id)
            {
                var value = GetSomeValue();
            }

            private string GetSomeValue()
            {
                // and here I want to return result to user not even returning result to calling method `Get`
                return "";
            }
        }

I can set status code to response through HttpContext.Response.StatusCode and I know how to set body to the response but I don't know either could I return response to user from GetSomeValue method or not. May be somehow using HttpContext?

Artyom
  • 654
  • 2
  • 7
  • 16

1 Answers1

0

I'm not really if this is what you're asking but here are my two cents:

I want to return result not from my public method that handles request but from an inner method.

Well this is not trivial, what you're asking If I get it right is to manipulate the HttpContext so you start the response on an inner method instead of returning from your controller, which is the right way.

I don't know why you would want to do that but, I guess you can get some advice here

In any case I don't get why don't you just:

return Ok(value);

like:

public class UsersController : MainController
{
    [HttpGet("{id:int}")]
    public IActionResult Get(int id)
    {
        var value = GetSomeValue();
        return Ok(value);
    }
    private string GetSomeValue()
    {
        // and here I want to return result to user not even returning result to calling method `Get`
        return "";
    }
}
Pablo Recalde
  • 3,334
  • 1
  • 22
  • 47
  • That was not what I would like to do [link](https://stackoverflow.com/questions/47350368/is-there-an-equivalent-to-httpcontext-response-write-in-asp-net-core-2). I can write response body but I can't return it not through `IActionResult`. I tried to minimize controller method that handles request via making some actions in inner small method. And now I understand that returning result from inner method is not clear so it's not a good practice but anyway I'm interested if it's possible – Artyom Sep 05 '19 at 06:25