CreatedAtAction doesn't work

Michel De Graeve 1 Reputation point
2021-05-12T21:06:05.167+00:00

I need to get a URL with CreatedAtAction but get a null.
My searches did not yield any results.

**My controller**

    namespace GeoService.Controllers
    {
        [Route("api/continent/{continentId}/country/{countryId}/City")]
        [ApiController]
        public class CityController : Controller
        {
            private readonly ICityRepository repoCity;
            CityMapper cityMapper = new CityMapper();
            private readonly ILogger logger;
            private readonly ILogger logger2;

            public CityController(ICityRepository repoCity, ILoggerFactory loggerFactor, ILogger<CityController> logger)
            {
                this.repoCity = repoCity;
                this.logger = logger;
                logger2 = loggerFactor.AddFile("./ControllerLog/City/CityControllerLogs.txt").CreateLogger("City");
            }

            [HttpGet("{id}")]
            public ActionResult<City> Get(int id)
            {
                try
                {
                    var city = repoCity.Get(id);
                    var result = cityMapper.Map(city, this.Url);
                    return Ok(result);
                }
                catch (CityException ex)
                {
                    return NotFound(ex.Message);
                }
            }

            [HttpPost]
            public IActionResult POST(int continentId, int countryId, [FromBody] City city)
            {
                try
                {
                    city.CountryId = countryId;
                    repoCity.Add(continentId, countryId, city);
                    return CreatedAtAction(nameof(Get), new {continentId = continentId, countryId = countryId, Id = city.Id}, cityMapper.Map(city, this.Url));
                }
                catch (CityException ex)
                {
                    return NotFound(ex.Message);
                }
            }


**My Map**

    public class CityMapper
        {
            public object Map(City city, IUrlHelper urlHelper)
            {
                return new
                {
                    CityId = urlHelper.ActionLink("Get", "City", new
                    {
                        id = city.Id
                    }),
                    Name = city.Name,
                    Population = city.Population,
                    Capital = city.Capital
                };
            }
        }

**The result in Postman** 
    {
        "cityId": null, <---- no URL
        "name": "New York",
        "population": 8336817,
        "capital": false
    }

    This must be the result
    {
        "cityId": "http://localhost:5000/api/continent/1/country/1/City/1",,
        "name": "New York",
        "population": 8336817,
        "capital": false
    }

What do I wrong.

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
293 questions
{count} votes