0

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?

DevP
  • 75
  • 2
  • 11
  • Does this answer your question? [How to use restsharp to download file](https://stackoverflow.com/questions/29123291/how-to-use-restsharp-to-download-file) – Leandro Bardelli Nov 11 '21 at 23:44

2 Answers2

1

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.

Dan Sorensen
  • 11,403
  • 19
  • 67
  • 100
1

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");
}
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
  • With this I am able to get the url content, but I can still see the url with api key in the – DevP Nov 12 '21 at 18:03
  • Then I've not explained it clear enough. You need to *remove* your existing ` – freedomn-m Nov 12 '21 at 18:18
  • Are you returning the string `"https://api-maps...."` from the Action? Don't do that, load the script *in the action* and return the content. I skipped that bit as it wasn't directly related to the principle of loading the script *server-side* then passing it. **edit** added as *basic* implementation to download the file *server-side* and send the *content* (not the url) back to the client. – freedomn-m Nov 12 '21 at 18:20
  • Thanks @freedomn-m, it works. Is there a way to call this action method from .js file as well? I have to use the key again in the .js file. OR Is there a way to keep the url "https://api-maps.yandex.ru/2.1/?apikey=xxxxxxxxxx&lang=ru_RU"; in another .js file and then use it in View ? – DevP Nov 12 '21 at 19:20
  • Once you've done `var contents = await ... readasstring...` you can manipulate the string all you like, eg `contents = contents.replace("https://api-maps...", "https://you_server/controller/action")`. If you need it again, consider a similar replacement. Essentially, you're creating a proxy server that adds the key at the server level. – freedomn-m Nov 12 '21 at 22:12
  • Is there a way to call the **Web API** action method instead of Controller Action method from .js file and return the javascript content? – DevP Nov 14 '21 at 20:06