Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!