Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Consume webAPi using .NET Core MVC

$
0
0

Hi All,

I am try to consume WebAPI using .NET Core MVC but i am getting below message

No connection could be made because the target machine actively refused it.

I have 2 Project in the Solution one is API and Other is Webproject to read API.

API Project

 [Route("api/[controller]")]
    [ApiController]
    public class EventController : ControllerBase
    {
        private readonly TAGCanadaDBDevContext _context;

        public EventController(TAGCanadaDBDevContext context)
        {
            _context = context;
        }

        // GET: api/<controller>
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Event>>> Get()
        {
            return await _context.Event.ToListAsync();
        }
    }

WEB Project

Helper.cs

public class EventAPI
    {
        public HttpClient GetEvent()
        {
            var client = new HttpClient();
            client.BaseAddress = new Uri("https://localhost:44361/");
            return client;
        }
    }

Controller

  EventAPI _api = new EventAPI();

        public async Task<IActionResult> Index()
        {
            IEnumerable<Event> evt;

            HttpClient client = _api.GetEvent();

            HttpResponseMessage responseTask = await client.GetAsync("api/event");   Getting error on this line       
            if (responseTask.IsSuccessStatusCode)
            {
                var readTask = responseTask.Content.ReadAsAsync<IList<Event>>();
                readTask.Wait();
                evt = readTask.Result;
            }
            else
            {
                evt = Enumerable.Empty<Event>();
                ModelState.AddModelError(string.Empty, "Error");
            }
            return View(evt);
        }

Thanks

Shabbir


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>