What is "Asp-fallback" for in ASP.NET MVC, Razor Pages?

Asked

Viewed 357 times

1

I see some files like lines similar to this:

<script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js"
        asp-fallback-src="~/lib/jquery/dist/jquery.min.js"
        asp-fallback-test="window.jQuery"
        crossorigin="anonymous"
        integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk">
</script>

I searched and found in English (I read using the translator), and it seems that it calls the second file if the first fails, but I do not know if it is that and nor for what purpose the asp-fallback-test or how they really work.

1 answer

2


When we are developing a web application one of the biggest concerns is the loading time of our application. One of the optimization techniques used is to use the so-called CDN for the most known libraries.

If the user has already visited a site that uses the same CDN file as you, this file is probably cached on his machine, which eliminates the need to download it again. This is good for the user who wins in speed and for you who decreases the load on your server.

But what if the CDN is down? We need a second option to download these files. ASP.NET fallback helpers make it easy and readable to do this.

  • asp-fallback-src is a second option for src of an html element
  • asp-fallback-href is a second option for src of an html element
  • asp-fallback-test is a verification condition that we can perform to know whether or not fallback will be used.

Remembering that fallback helpers can be used to apply Polyfill techniques.

Are there any options of fallback taghelpers that you can find on official documentation

Browser other questions tagged

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