0

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?

Don Box
  • 3,166
  • 3
  • 26
  • 55
  • You need to browse websites via a local HTTP server. – ColinM Dec 10 '19 at 14:47
  • @ColinM Thanks, I already know that :) That's not what I am asking. Please let me know if question was not clear. Please note I mentioned that I implement an "export to offline HTML" feature. – Don Box Dec 10 '19 at 14:48
  • @the_lotus I mentioned it how – Don Box Dec 10 '19 at 15:59
  • Try "." instead... so "~/Scripts/file.js" becomes "./Scripts/file.js" Just make sure when exporting that all pages are in the same folder. Where this might become a problem is when two pages have the same name but are in different "folders" in your project. – pcalkins Dec 10 '19 at 20:55
  • @pcalkins Thanks but changing the paths in the web app like this won't work. The scripts folder is not within the razor views folder. It will break the web app. – Don Box Dec 11 '19 at 05:56
  • I wonder if I can't inject somehow in the process of how ASP.NET transforms the "~" tilda into "/". Something maybe related to HttpContextWrapper. I haven't been able to find something – Don Box Dec 11 '19 at 06:03
  • https://stackoverflow.com/questions/4563235/where-does-asp-net-virtual-path-resolve-the-tilde shows how ~ gets transformed but it doesn't help much. Also https://stackoverflow.com/questions/4350571/changing-asp-net-application-root says about VirtualPathProvider but I don't know how I can use VirtualPathProvider just when I render the Razor view to string – Don Box Dec 11 '19 at 06:52
  • remember that when the pages are compiled they are running from the root folder (wwwroot), not the 'pages' or views folder. – pcalkins Dec 11 '19 at 19:21
  • can you describe the requirements for the export to offline feature here? I think that might help you get a better answer here. (for instance are you going to export a local copy of your database, or is all the data going to be in the HTML) – pcalkins Dec 11 '19 at 19:40
  • @pcalkins using the dot `./Scripts/file.js` won't work in a view like `/SomeController/SomeAction` because the HTML expects to find `SomeController/SomeAction/Scripts/file.js` – Don Box Dec 12 '19 at 11:26
  • Regarding the export: it's expected that all the files used by the exported HTML, like JS, images, etc. to be near the HTML so that loading the HTML browser will work. My goal was to find a way so that when I expert the razor view, I can somehow change the paths used by `script` and `link` tags on the fly so that the exported HTML will work – Don Box Dec 12 '19 at 11:28
  • Another option I see is if I change all the URLs in the web app to be relative to the razor view `../../Scripts/file.js`, instead of using the root relative path `~/Scripts/file.js`. But I am not sure about this, it looks tedious having to compute these paths when writing code. And I am not sure if I don't get in trouble actually, there might be scenarios where it just won't work, not sure – Don Box Dec 12 '19 at 11:32
  • the path is not actually "/SomeController/SomeAction"... this is routing... but the controller and method would not be available anyway because there is no server in offline mode. – pcalkins Dec 12 '19 at 17:33
  • btw, for maximum portability you might consider including the scripts in the markup along with base64 encoded images. – pcalkins Dec 12 '19 at 18:26
  • @pcalkins Here's how the "export" works: I'm rendering one Razor view to a HTML file and I am saving images and scripts near the HTML (I am trying to use same folders to make it easier). Changing the Razor view to using dot "./" in script and link tags will break the web app. – Don Box Dec 18 '19 at 10:51
  • You'll need to fix the paths after exporting. Routing will not happen in offline mode. – pcalkins Dec 18 '19 at 19:26

0 Answers0