locked
Consume webAPi using .NET Core MVC RRS feed

  • Question

  • User-1256377279 posted

    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

    Monday, February 24, 2020 7:52 PM

Answers

  • User711641945 posted

    Hi shabbir_215,

    Firstly,you need to set multiple startup projects.Here is the steps:

    1.In Solution Explorer, select the solution (the top node).

    2.right-click your solution and then choose Properties. The Solution Property Pages dialog box appears.

    3.Choose the Multiple Startup Projects option and set the appropriate actions(like the picture,choose start).

    Secondly,be sure the url is right.The api port you called should be like below:

    1.check the request url in launchSettings.json in your Web Api project:

    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:58638",
          "sslPort": 44338
        }
      },
    //...
    }

    2.In your WebProject:

    client.BaseAddress = new Uri("https://localhost:44338/");
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 25, 2020 9:31 AM

All replies

  • User1120430333 posted

    This happens becuase you didn't set the WebAPI project as the Startup Project and start  'Start without Debug' the project actually run it. So it of course the WebAPI service is not going to be listening on the port. After you start the WebAPI project, you then need to set VS for the client  project as the Startup project and start it.

    Monday, February 24, 2020 9:41 PM
  • User1064314092 posted

    at fast Open new API project. then again open the new VS studio. and open a new Web Project. then run 2 projects at the same time. or fast run API project then web (but must be 2  projects is run)

    if any confusion, see this photo (run 2 project)

    https://i.stack.imgur.com/Rkyhp.png

    and 

    GetAsync("Get") /// this
    //// Call api action name or action method

    GetAsync("api/event") //Not this

    Tuesday, February 25, 2020 4:35 AM
  • User711641945 posted

    Hi shabbir_215,

    Firstly,you need to set multiple startup projects.Here is the steps:

    1.In Solution Explorer, select the solution (the top node).

    2.right-click your solution and then choose Properties. The Solution Property Pages dialog box appears.

    3.Choose the Multiple Startup Projects option and set the appropriate actions(like the picture,choose start).

    Secondly,be sure the url is right.The api port you called should be like below:

    1.check the request url in launchSettings.json in your Web Api project:

    {
      "$schema": "http://json.schemastore.org/launchsettings.json",
      "iisSettings": {
        "windowsAuthentication": false, 
        "anonymousAuthentication": true, 
        "iisExpress": {
          "applicationUrl": "http://localhost:58638",
          "sslPort": 44338
        }
      },
    //...
    }

    2.In your WebProject:

    client.BaseAddress = new Uri("https://localhost:44338/");
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, February 25, 2020 9:31 AM
  • User-1256377279 posted

    Thanks Rena Ni it worked :) 

    Tuesday, February 25, 2020 11:02 AM
  • User1120430333 posted

    Thanks Rena Ni it worked :) 

    The other thing you could have done is simply published the WepAPI to local IIS. It will always be in an executing state. And you don't need VS to start it. And you can debug too.

    https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-enable-debugging-for-aspnet-applications?view=vs-2017

    Tuesday, February 25, 2020 3:00 PM