MVC2 IIS - 在部署时正确解析外部样式表和脚本中的 URL?
我将 Visual Studio MVC2 项目更改为在本地 IIS 服务器上进行调试,但遇到了 URL 解析方式问题。
我现在必须像这样使用 ResolveUrl() 方法,而不是对 ajax 请求的链接进行硬编码:
<%= ResolveUrl("~/Controller/Action")%>
当 javascript 存在于视图中时,这很好。但我在外部 .js 文件中有 javascript,我无法在其中使用 ResolveUrl() 函数。
此问题还会影响外部样式表中图像的链接。
我觉得每次从 Visual Studio Web 服务器移动到本地 IIS Web 服务器,再到实际部署的 Web 服务器时,我不需要手动更改这些链接。
解决这个问题的最佳方法是什么?
到目前为止,我能想到的就是使用相对路径,例如 ../Content/Images/image.gif
I changed my Visual Studio MVC2 project to debug on a local IIS server, but I ran into problems with how the URLs are resolved.
Instead of hard-coding the links for say an ajax request, I now have to use the ResolveUrl() method like so:
<%= ResolveUrl("~/Controller/Action")%>
Which is fine when the javascript lives in a view. But I have javascript in external .js files which I can't use the ResolveUrl() function inside of.
This problem also affects links to images within external style sheets.
I feel like I shouldn't need to manually change these links every time I move from the Visual Studio web server to a local IIS web server, to an actual deployed web server.
What is the best way to work around this?
So far all I can think of is using a relative path such as ../Content/Images/image.gif
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以给你一些建议。首先,您不应该使用 ResolveUrl 来解析控制器操作,因为您仍在硬编码并且没有利用路由。如果明天您决定更改 global.asax 中的路线定义怎么办?您应该像这样使用 Url 帮助器:
对于静态资源:
现在,当您需要在 CSS 文件中定义图像路径时,您应该知道它们可能相对于 CSS 文件的位置。例如,如果您像这样包含 CSS:
在
foo.css
内,您可以使用相对图像路径:最后一部分是 javascript 文件。在它们内部你不能使用助手来定义 url。因此存在不同的技术。就我个人而言,我喜欢带有渐进增强功能的不显眼的 javascript,这意味着我需要的 url 已经在 HTML 中了。例如,考虑 AJAX 化链接或表单。在您的视图中,您将拥有:
在单独的 javascript 中,我们可以对其进行 AJAX 化:
但在某些情况下,您在 DOM 中没有 url。因此,您可以在视图内使用全局变量:
并在外部 javascript 内使用此
myurl
全局变量。Here are a few tips I can give you. First of all you should not use
ResolveUrl
for resolving controller actions because you are still hardcoding and not taking advantage of the routes. What if tomorrow you decide to change your route definitions in global.asax? You should use Url helpers like this:and for static resources:
Now when you need to define image paths in CSS files you should know that they could be relative to the location of the CSS file. So for example if you include your CSS like this:
inside
foo.css
you could use relative image paths:and the last part are javascript files. Inside them you cannot use helpers to define urls. So different techniques exist. Personally I like unobtrusive javascript with progressive enhancement meaning that the url I need is already in the HTML. Think for example AJAXifying a link or a form. Inside your view you would have:
and inside a separate javascript we could AJAXify it:
There are case though where you do not have the url in the DOM. So you could use a global variable inside the view:
and inside your external javascript use this
myurl
global variable.所以我建议在你的 c# 文件中你有一个隐藏的文本字段,你在那里写下绝对 url 路径,并在从文本文件读取后将你的路径附加到外部 js 文件。
但我确实没有看到从 js 文件加载另一个 js 文件有任何好处。只需一次加载所有文件,然后在 yoiur js 文件中有条件地使用它即可。我假设您正在从 js 文件本身加载其他 js 文件,因为您想要在加载 js 文件时执行一些操作。您只需在第一个文件的 window.load 上调用下一个文件中的函数即可实现相同的目的。这就是您的最终目标吗?
我认为你不明白我在做什么。
假设您必须进行回发才能执行控制器 Food 和操作 GetMenu 那么在您的 aspx 页面中您将有一个隐藏字段,其值设置如下:
关于您的 css 和 js,您可以按如下方式获取它们:
或再次存储在隐藏字段,然后使用它。
希望有帮助
So what I would suggest is in your c# file you have an hidden text field and you write the absolute url path there and you append your path to the external js file after reading from the text file.
But I really dont see any gains out of loading another js file from a js file. Just load all the files at once and us eit conditionally within yoiur js file. I am assuming that you are loading the other js file from the js file itself cos you want to perform some action on loading the js file. you can achieve the same by just calling the function in the next file on window.load of the first file.. is that what your ultimate goal is?
I dont think you understood what I was getting into.
Say you have to do a postback to do a controller Food and action GetMenu then in your aspx page you will have a hidden field with the value set as follows:
and regarding your css and js you can get them as follows:
or again store in a hidden field and then use it.
Hope that helps