.net 6 mvc with react ERR_CONNECTION_REFUSED

Viewed 46

console error is ERR_CONNECTION_REFUSED and Uncaught (in promise) AxiosError {message: 'Network Error', name: 'AxiosError', code: 'ERR_NETWORK' ~

like this: enter image description here

controller

[ApiController]
    [Route("api/[controller]")]
    public class AccountController : ControllerBase
    {
        private readonly IHaruUserRepository _repository;
        private readonly ILogger _logger;

        public AccountController(IHaruUserRepository repository, ILoggerFactory loggerFactory)
        {
            this._repository = repository ?? throw new ArgumentNullException(nameof(AccountController));
            this._logger = loggerFactory.CreateLogger(nameof(AccountController));
        }

        [HttpGet] // [HttpGet("[action]")]
        public async Task<IActionResult> GetAll()
        {
            try
            {
                var models = await _repository.GetAllAsync();
                if (!models.Any())
                {
                    return new NoContentResult();
                }
                return Ok(models);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return BadRequest();
            }
        } 

function

async handleSubmit(e) {

        e.preventDefault();

        await axios.get("/api/account").then(result => {

        });
}
1 Answers

I added 'services.AddTransient<'interface name of repository', 'repository name'>();' in DI Container.

Related