How to place a url on the web.config

Asked

Viewed 507 times

-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 not leave the url sinker in the code, as is?

This way gives me error(I tried to do as the 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;

picked it up

An invalid request URI was provided. The request URI must either be an Absolute URI or Baseddress must be set.

  • When concatenates the url + parametro what is the result? Make a Quickwatch and validates the value.

2 answers

1

I guess I could put it on the 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.

  • So I did something similar and when I called the url and added the ok route, but when I called the route + parameter then it didn’t work

  • The route + parameter, how so? You can put the code of what you were doing in your question?

  • I put an edit in the post, including an example of yours. In web.config it’s just this: http://localhost:56137/api/

  • @pnet you need to put the details before the author reply, man. I’ve warned before that this is really bad. Avoid the "chameleon questions" because it impairs the answer and it seems that the person who answered gave a wrong or incomplete answer.

-2

I resolved so:

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;
        }
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.