locked
WebScraping in asp.net core 3.1 RRS feed

  • Question

  • User-189547558 posted

    Hello guys. I'm trying to make a web scraper, but this error appears in GetAsync (). Does anyone know for what reason?

    Thanks

    The code:

    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Http;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.Extensions.Logging;
    using AngleSharp;
    using AngleSharp.Html.Parser;
    
    namespace web_scraper.Controllers
    {
        [Route("[controller]")]
        [ApiController]
        public class WebScraperController : ControllerBase
        {
            private readonly ILogger<WebScraperController> _logger;
    
            public WebScraperController(ILogger<WebScraperController> logger)
            {
                _logger = logger;
            }
    
            [HttpGet]
            public async Task<string> GetAsync() //O erro aparece aqui em GetAsync()
            {
                var config = Configuration.Default.WithDefaultLoader();
                var context = BrowsingContext.New(config);
                var document = await context.OpenAsync("https://www.americanas.com.br/produto/1951947793?opn=YSMESP&sellerid=23060006000150&epar=bp_pl_00_go_tel_smartphone_todas_geral_gmv&WT.srch=1&acc=e789ea56094489dffd798f86ff51c7a9&i=5d32878c49f937f6252da60d&o=5f3bf3b0f8e95eac3dd8a860&gclid=CjwKCAiAhbeCBhBcEiwAkv2cYx7Q2RhUaBWOLTd3CimKTQeF7d3uum9gohFS2DeUICC_7lvivUHmyBoCOn4QAvD_BwE");
                _logger.LogInformation(document.DocumentElement.OuterHtml);
            }
        }
    }

    The error mensage:

    "WebScraperController.GetAsync()": nem todos os caminhos de código retornam um valor

    Monday, March 15, 2021 12:26 PM

Answers

All replies

  • User475983607 posted

    The error, "Not all code paths return a value",  is very clear.  You've defined the action method to return a string but the method body does not return a string. 

    C# Programming Guide.

    https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 15, 2021 12:31 PM
  • User-189547558 posted

    thanks, with your comment i managed to understand the code better and fixed the error.

    Monday, March 15, 2021 2:18 PM
  • User-189547558 posted

    The right code:

            [HttpGet]
            public async Task GetAsync()
            //public async Task<float> GetAsync()
            {
                var config = Configuration.Default.WithDefaultLoader();
                var context = BrowsingContext.New(config);
                var document = await context.OpenAsync("https://deals.dell.com/pt-br/productdetail/6fmh");
                _logger.LogInformation(document.DocumentElement.OuterHtml);
            }

    Monday, March 15, 2021 2:20 PM