Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
Asp.Net MVC view how to add script src from backend?

I have a MVC view where I am using Yandex external API for address search.

This is the script I have to add in order to use the Yandex services.

<script src="https://api-maps.yandex.ru/2.1/?apikey=xxxxxxxxxx&lang=ru_RU" type="text/javascript">

Is there a way to read this script src from the backend instead of directly using it in the View, as it contains the api key?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use a server side method to 'read' the script src file from the ASP.Net server. You would perform an HTTP GET using something like System.Net.Http.HttpClient. This is effective if you are retrieving JSON data or some other type of data to process on the server.

However, the C# backend would not process JavaScript instructions. For that you would need a server side JavaScript engine such as Node, or the client's web browser.

Including the JavaScript file in the View will allow the client's JavaScript engine to handle processing. But as you have implied, nothing in the View is hidden from the client.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create an Action that loads and returns the script, then reference that Action in your html, eg:

<script src='@Url.Action("LoadYandex")'>

and

public ActionResult LoadYandex() {
    // HttpClient to get the file
    return Content(file, "text/javascript");
}

Edit more detailed Action example, basic "get file from server and return contents". Look for more detailed SO answers to include error checking etc

public async Task<ActionResult> LoadYandex()
{
    var url = "https://api-maps.yandex.ru/2.1/?apikey=xxxxxxxxxx&lang=ru_RU"; 

    var httpClient = new HttpClient();
    var response = await httpClient.GetAsync(url);
    var contents = await response.Content.ReadAsStringAsync();

    return Content(contents, "text/javascript");
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda