What is the actual problem
Solution for Views
Inside razor views this is simple and nothing new to use the
UrlHelper class. For example to get the proper URL for an image we would type
Url.Content("~/images/logo.png"). This would automatically generate the proper URL no matter if the application is hosted in a subfolder or not.
There actually turns out to be a HTML Element for exactly this purpose - the
<base> HTML Element.
So my solution looks like this, inside your
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <base href='@Url.AbsoluteContent("~/")'> <title>@ViewBag.Title - ASP.NET Core Web Application</title> <!-- ... --> </head> <body>
AbsoluteContent is an extension method that I added to my application to get the full URL and not only the path.
var baseUrl = document.baseURI;
- Insert a
<base>element with a
hrefattribute into the the
- Get that attribute value from the script
In my opinion this is a clean and simple solution.comments powered by Disqus