Home > database >  How to get ActionResult StatusCode in ASP.NET Core
How to get ActionResult StatusCode in ASP.NET Core

Time:09-05

I have an API whose return type is ActionResult:

[HttpPost("UploadFiles")]
public async Task<ActionResult> MyFunction(MyFunctionInput input) 
{
    if (input.Id == null) 
    {
        retrun BadRequest("Id must exist");
    }

    // Do some stuff

    return Ok();
}

I am calling this function from another API in same class and storing the result in a variable. But when ever I am trying to get the StatusCode from the result, it says ActionResult does not contain a definition for StatusCode like this

var response = await MyFunction(attInput);

if (response.StatusCode == System.Net.HttpStatusCode.NotFound || 
    response.StatusCode == System.Net.HttpStatusCode.BadRequest)
{
    //To Do
}

I googled it but almost everyone said to convert the result to OkObjectResult.

What should I do now?

CodePudding user response:

Your question need more explain !!! How to call ? you use HttpClient ? or call by Ajax ? Please edit your question by answer me questions.

Generally explain how to get states code in below

If you want request anther api in backend use HttpClient

// TODO: make request
var response = await client.GetAsync(url);

// get status code 
if(response.StatusCode == StatusCode.Ok) // when your endpoint success state
{
    //TODO: write code here 
}

Make HTTP requests using IHttpClientFactory in ASP.NET Core

CodePudding user response:

Firstly,as you said,ActionResult does not contain a definition for StatusCode.If you want to get StatusCode with response.StatusCode,you can try to use HttpClient as Zanyar J.Ahmed shown.If you want to get StatusCode with await MyFunction(attInput);,you can try to set statuscode with Session in MyFunction.

Startup.cs:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddDistributedMemoryCache();

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromSeconds(10);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true;
        });

       ...
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        ...
        app.UseAuthentication();
        app.UseAuthorization();

        app.UseSession();

        ...
    }

api:

[HttpPost("UploadFiles")]
public async Task<ActionResult> MyFunction(MyFunctionInput input) 
{
    if (input.Id == null) 
    {
        HttpContext.Session.SetInt32("StatusCode", 400);
        retrun BadRequest("Id must exist");
    }

    // Do some stuff
    if (HttpContext.Session.GetInt32("StatusCode") == null)
            {
                HttpContext.Session.SetInt32("StatusCode", 200);
            }
    return Ok();
}

get status code:

var response = await MyFunction(attInput);

if (HttpContext.Session.GetInt32("StatusCode")=400)
{
    //To Do
}
  • Related