Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
How to dynamically call an ActionResult in your API by using an Ajax-call?

I have coded in the StartUp.cs the following code for calling my API.

            services.AddHttpClient("MyApi", c =>
            {
#if DEBUG
                c.BaseAddress = new Uri("https://localhost:12345");
#else
                c.BaseAddress = new Uri("https://myApi.com");
#endif

But when I want to call the ActionResult by using an Ajax-call, he can't find the API.

    alert(apiUrl);

    $.ajax({
        url: apiUrl + '/MyApiProcesses/GetSomething',
        type: 'POST',

So I have written this variable in a js-file.

var apiUrl = 'https://localhost:12345';
//var apiUrl = 'https://myApi.com';

I want to know if it is possible to write it dynamically. If you declare it in the startup, you don't have to declare it twice?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If you need to use urls in ajax or httpclient, I usually do it this way, but it takes several steps to get a string from appsettings.

  1. Create AppUrl section in appsettings.json
"AppUrl": {
    "DevUrl": "http//..",
    "ProductUrl": "http//..",
     .... another urls if needed
   
},

2.Create class for this section

 public class AppUrlSettings
  {
        public string DevUrl{ get; set; }
        public string ProdUrl{ get; set; }

        ....another urls
   }
  1. configure settings in startup
var appUrlSection=Configuration.GetSection("AppUrl");

services.Configure<AppUrlSettings>(appUrlSection);

var urls = appUrlSection.Get<AppUrlSettings>();

services.AddHttpClient("MyApi", c =>
 {
#if DEBUG
                c.BaseAddress = new Uri(urls.DevUrl);
#else
                c.BaseAddress = new Uri(urls.ProdUrl;
#endif
});
  1. now you can use them like this
public class MyController:Controller
    {
        private readonly IOptions<AppUrlSettings> _appUrls;

        public MyController (IOptions<AppUrlSettings> appUrls)
        {
            _appUrls = appUrls;
        }

        public  IActionResult MyAction()
        {
           var model= new Model
           {
            DevUrl=_appUrls.Value.DevUrl;
               ...
           }
        }
}

you can then use urls as hidden fields.

or you can get urls from model in javascript directly:

var devUrl = @Html.Raw(Json.Encode(@Model.DevUrl));
.....

Or if you need the urls in many places, it can make sense to create a special service that you can inject directly in the views you need

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda