相对图像URL中的JavaScript文件 - ASP.net MVC和IIS 7 [英] Relative Image URL in Javascript File - ASP.net MVC and IIS 7

查看:113
本文介绍了相对图像URL中的JavaScript文件 - ASP.net MVC和IIS 7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发一个Asp.net MVC应用程序,一切工作在ASP.net开发服务器正常,但当我想其托管在IIS 7我收到有关URL解析问题。我曾在Javascript中使用相对路径,使图像。该脚本文件是在〜/脚本/文件夹和图像文件都在〜/内容/图像/文件夹中。现在,在javascript文件我想通过给喜欢的相对路径使用给予
HTTP://本地主机/ WebApp1 /控制器1 /动作1 /
它试图在 HTTP找到它://本地主机/控制器1 /动作1 / 和couldn;吨找到该文件。


解决方案

如果怎么我读这是正确的,你现在从IIS7的现在,而不是WebApp1的域根从开发环境中运行Web应用程序的<? / p>

所以,如果我的假设是正确的,然后点击〜/ 现在应该解决为 HTTP://本地主机/ 而不是 HTTP://本地主机/ WebApp1 /

如果这是所有仍然是正确的,那么你的文件夹结构具有全部转移到上一级您的〜/脚本的绝对路径文件夹:

的http://本地主机/脚本/

和图像的文件夹:

的http://本地主机/内容/图片/

为了从您的脚本访问您的图像,你可以使用多种方法。最简单的就是使用相对路径从你的脚本目录:../内容/图片/ myImage.jpg这个参数

或者您可以使用document.location.host给javascript内部建立的完全限定路径名称: document.location.host +/Content/Images/MyImage.jpg

另一种方法是让ASP.NET构建脚本的这部分动态,以便完全合格的路径名injected.You可以使用的 ScriptManager.RegisterStartupScript 的ScriptManager做到这一点。 RegisterScriptBlock

有真的很多方法对皮肤这只猫,他们只是前3我能想到的把我的头顶部。

I am developing an Asp.net mvc application, everything works fine in ASP.net development server but when I am trying to host it on IIS 7. I am getting problem related to URL resolution. I have used relative paths in Javascript to give the image. The script file is in ~/Scripts/ folder and image files are in ~/Content/images/ folder. Now in the javascript file I am trying to use the give by giving the relative path like http://localhost/WebApp1/controller1/action1/ it tries to find it at http://localhost/controller1/action1/ and couldn;t find the file.

解决方案

If how I'm reading this is correct, you're now running the WebApp from the domain root of IIS7 now instead of WebApp1 from your development environment?

So, if my assumption is correct, then ~/ should now resolve to http://localhost/ instead of http://localhost/WebApp1/

If that is all still correct, then your folder structure has all moved up one level with your ~/scripts folder in the absolute path:

http://localhost/Scripts/

and your images folder as:

http://localhost/Content/Images/

In order to access your images from your scripts, you can use a number of methods. The simplest is to use the relative path from your scripts directory: "../Content/Images/MyImage.jpg"

Or you can use document.location.host to build the fully qualified path name within the javascript: document.location.host + "/Content/Images/MyImage.jpg"

Another method is to have ASP.NET build this part of the script dynamically so that the fully qualified path name is injected.You can do this by using ScriptManager.RegisterStartupScript or ScriptManager.RegisterScriptBlock

There's really many ways to skin this cat, they're just the first 3 I can think of off the top of my head.

这篇关于相对图像URL中的JavaScript文件 - ASP.net MVC和IIS 7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆