How to put a url in web.config

-2

I have a service that calls this service:

public async Task<List<Funcionario>> GetFuncionarios()
        {
            string url = $"http://localhost:56137/api/GetFuncionario";
            var response = await client.GetStringAsync(url);
            var _funcionario = JsonConvert.DeserializeObject<List<Funcionario>>(response);

            return _funcionario;
        }

How do I stop the url in the code, how are you?

That way it gives me error (I tried to do as colleague JJoão passed me)

var webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

            string parametro = "GetCidade/{id}";

            //if (webConfig.AppSettings.Settings.Count > 0)
            //{
                var customSetting = webConfig.AppSettings.Settings["serviceApi"];
                string url = customSetting?.Value;

                var response = await client.GetStringAsync(url + parametro);
                var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);

                return _cidade;

I get this

  

An invalid request URI was provided. The request URI must either be an   absolute URI or BaseAddress must be set.

    
asked by anonymous 14.08.2018 / 09:51

2 answers

1

I think I could put in the part of appSettings of web.config :

<appSettings>
  <add key="myUrl" value="http://localhost:56137/api/GetFuncionario"/>
</appSettings>

Then to get the value:

var webConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);

if (webConfig.AppSettings.Settings.Count > 0)
{
    var customSetting = webConfig.AppSettings.Settings["myUrl"];
    string url = customSetting?.Value;

    // tratar URL...
}

It would be something like that.

    
14.08.2018 / 11:38
-2

I solved it like this:

public class GetCidadesAsync
    {
        HttpClient client = new HttpClient();
        string url = ConfigurationManager.AppSettings["serviceApi"];

        public async Task<List<Cidade>> GetCidades()
        {
            string parametro = $"GetCidade";
            var response = await client.GetStringAsync(url + parametro);
            var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);           

            return _cidade.ToList();
        }

        public async Task<List<Cidade>> GetCidadeById(int id)
        {
            string parametro = $"GetCidade/{id}";
            var response = await client.GetStringAsync(url + parametro);
            var _cidade = JsonConvert.DeserializeObject<List<Cidade>>(response);

            return _cidade;
        }
    }
    
14.08.2018 / 13:00