2016-04-22 82 views
1

在我的MVC应用程序,我想成为一个html页面类似这样的决心使用Server.Mappath引用的文件路径相对DIR

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

入口点的index.htm包括一些JS脚本。

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

即使我指定了相对URL,包含的脚本的路径也被解析到应用程序根目录。

指定绝对路径有效,但由于动态生成的目录名称,我需要脚本来引用相对位置。

我知道我没有提供足够的信息。 我认为发生的事情是从索引控制器返回.htm文件,其路由是应用程序根目录。这就是为什么相对路径如此解决。

+1

请说明你/想要的网址。 “./whver.js”实际上是相对于当前页面的url - 所以不太清楚你期望它解决什么。 –

+0

备注:MVC标签适用于MVC设计模式问题 - 从我所能看到的与您的帖子完全无关。可能你的意思是'asp.net-mvc'。 –

+0

我希望它解决到文档下的动态生成的文件夹,在我的情况/Documentation/791/whver.js,但由于某种原因我得到localhost/whver.js –

回答

1

我希望如果你改变从相对到绝对的路径,它会更容易。绝对路径以斜线开头 - '/'并显示您网站的根目录。我的意思是,当您访问website.com/index.html网页的绝对路径是/index.html时。

下一个代码适用于我。 public ActionResult Index() { using (var streamRead = System.IO.File.OpenRead( System.IO.Path.Combine( Server.MapPath("~/Documents"), "index.html"))) { return File(streamRead, "text/html"); } }

和HTML页面: <!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>

+0

在我的问题中,我指定绝对路径有效,但我有动态生成的文件夹 –