Don Box
Don Box

Reputation: 3336

Make root-relative URLs also work when HTML is loaded from disk

In the app I'm working on, the script and link tags in the Razor view use the tilda slash in the path, which is common in ASP.NET MVC 4 apps:

<script type="text/javascript" src="~/Scripts/file.js"></script>

which as expected, produce the HTML with root-relative URLs:

<script type="text/javascript" src="/Scripts/file.js"></script>

Based on the solution from here, I need to implement an 'export to offline HTML' feature, which is meant to create offline HTML file out of a Razor view so that users can load the HTML from their disk.

The issue

The issue is with the root-relative path. When I load the exported HTML file from disk, the script tag above will try to load from "file:///C:/folder/Scripts/file.js" where "C:/folder/" is the location of the HTML file I created.

What's the simplest/best way in order to make work for both when running in ASP.NET and as a local file?

Ideas

I could just change the path in the Razor view to a relative URL like:

<script type="text/javascript" src="Scripts/file.js"></script>

but that's not what I want, as it can have different implications.

Is there a way to make the rendering the Razor view to HTML in a way so that it produces relative path from tilda slash?

How does ASP.NET convert the tilda slash "~" to the root-relative path "/" ? Can I override that?

Upvotes: 0

Views: 272

Answers (0)

Related Questions