1

In my mvc application, I'm trying to serve an html page like this

return File(Server.MapPath("~/Documentation/" + decoded["version"].ToString().Trim() + "/" + "index.htm"), "text/html");

The entry point index.htm includes some js scripts.

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

Even though I specified the relative url, the path of the included script is getting resolved to the application root.

Specifying an absolute path works, but I need scripts to reference a relative location because of a dynamically generated directory name.

I understand I provided not enough information. I think what is happening is that the .htm file is returned from the Index controller and its route is the application root. Thats why relative paths are resolved like that.

tergd1
  • 557
  • 6
  • 19
  • 1
    Please clarify what urls you get/want. "./whver.js" is indeed url relative to current page - so not really clear what you expect it to resolve to. – Alexei Levenkov Apr 22 '16 at 15:53
  • Side note: MVC tag is for MVC design pattern questions - it is completely unrelated to your post from what I can see. Possibly you mean `asp.net-mvc`. – Alexei Levenkov Apr 22 '16 at 15:55
  • I expect it to resolve to the dynamically generated folder under Documentation, in my case /Documentation/791/whver.js but Im getting localhost/whver.js for some reason – tergd1 Apr 22 '16 at 15:57
  • It sounds like you expect browser to know how server renders the page. I think you need to spend some time understanding what code/functionality runs on server and what comes from browser... In current state of the post it is not possible to provide reasonable answer (also indeed it is quite obvious that page is actually rendered at something like "/page.html" based on puzzle pieces you've hinted about - you'd have to edit it in the post before anyone can provide answer) – Alexei Levenkov Apr 22 '16 at 16:04

1 Answers1

1

I hope if you change path from relative to absolute it will be easier. The absolute path starts with slash - '/' and shows the root point of your website. I mean when you go to the website.com/index.html absolute path of your page is /index.html.

Next code is work for me. public ActionResult Index() { using (var streamRead = System.IO.File.OpenRead( System.IO.Path.Combine( Server.MapPath("~/Documents"), "index.html"))) { return File(streamRead, "text/html"); } }

And html page: <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> </head> <body> <script type="text/javascript" src="/Scripts/jquery-1.10.2.js" ></script> <script> console.dir("jquery version: " + jQuery.fn.jquery); </script> </body> </html>

Liashenko V
  • 233
  • 2
  • 11
  • In my question I specified that the absolute path works, but I have a dynamically generated folder – tergd1 Apr 22 '16 at 16:39