How to remove Metadata from API Response (Multi Language - ASP.NET CORE)

38 views Asked by At

I would like my API response to return multi-language messages. Everything works well, however in my API response its returning the metadata such as below:

Error: response status is 404
Response body
{
 "name": "No Data",
  "value": "Data Not Found",
  "resourceNotFound": false,
  "searchedLocation": "API.Resources.SharedResources"
}

My question is how do I remove "resourceNotFound": true, "searchedLocation": "xxx.Controllers.ProfileController", and "name": "No Data" with only returning the value?

This my code:

program.cs

builder.Services.AddLocalization();

var localizationOptions = new RequestLocalizationOptions();

var supportedCultures = new[]
{
    new CultureInfo("en-US"),
    new CultureInfo("id-ID")
};

localizationOptions.SupportedCultures = supportedCultures;
localizationOptions.SupportedUICultures = supportedCultures;
localizationOptions.SetDefaultCulture("en-US");
localizationOptions.ApplyCurrentCultureToResponseHeaders = true;

profile controller

private readonly IStringLocalizer<SharedResources> _localizer;
public ProfileController(IStringLocalizer<SharedResources> localizer)
        {
            _localizer = localizer;
          
        }

[HttpGet("home-company")]
        ... 

            if ( company != null)
            { 
                return Ok(complist);
            }
            return NotFound(_localizer["No Data"]);

Appreciate on any help.

1

There are 1 answers

0
Ruikai Feng On

just modify this line:

return NotFound(_localizer["No Data"]);

to

return NotFound(_localizer["No Data"].Value);