我同时使用 asp 和 asp.net 页面。我想在本地 iis (v5.1) 中托管该应用程序,但后来了解到 iisexpress 适合我的需求。但无论我使用 iis 5.1 还是 iisexpress,我似乎都有一个问题。
我使用的 asp 页面引用驻留在不同虚拟目录中的静态资源(css、javascript 等)。例如,一个 css 文件包含看起来像这样。
<link rel="stylesheet" href="/common/include/style/css.css"/>
如果这样的事情应该从测试环境运行,那么上面的 url 将解析为:
http://testing/common/include/style/css.css
这与我的主应用程序所在的位置形成鲜明对比。看起来像这样:
http://testing/myapp/default.aspx
现在,如果我在端口 8082 中运行 iisexpress,并且有一个入站请求,例如:
http://localhost:8082/common/inlcude/style/css.css
它将遇到 404 错误。是否可以指示 iss 或 iis express 解析此类 url(以 /common/...
开头),表示 http://testing/common/...
更新(2011 年 5 月 31 日,晚上 7 点 IST):
一直在研究什么是 url 重写,并且从示例中我开始了解一些事情。我不确定我想要的是否是 url 重写本身。再次以 iisexpress 进行类比,我知道会有一个入站请求 uri,例如:
http://localhost:8082/common/inlcude/style/css.css
但我希望实际上由以下 uri 提供服务:
http://testing/common/include/style/css.css
前一个 uri 不存在于我使用 iisexpress 虚拟化的文件夹中。
我需要在这里重写url吗?
此外,在 ASP 中,我包含如下行:
<!-- #include virtual="/common/include/classes/utils.asp" -->
即使这些东西也应该解析为它们相应的 http://testing/...
对应项。
ps:我做的这一切都是iis 5.1
I am working with both asp and asp.net pages together. I wanted to host the application in my local iis (v5.1) but later learned about iisexpress suits my needs. But irrespective of whether I use iis 5.1 or iis express I seem to have an issue.
The asp page which I work with refers to static resources (css, javascript, etc) which reside in a different virtual directory. For e.g. a css file include would look like this.
<link rel="stylesheet" href="/common/include/style/css.css"/>
If such a thing is supposed to run from the test environment then the above url would resolve to:
http://testing/common/include/style/css.css
This is in contrast where my main application would reside. That would look something like:
http://testing/myapp/default.aspx
Now if I run iisexpress in say port 8082, and there is an inbound request like:
http://localhost:8082/common/inlcude/style/css.css
it will hit a 404 error. Is it possible to instruct iss or iis express to resolve such url (which begin with /common/...
) to say http://testing/common/...
Update (May 31st 2011, 7.04 PM IST):
Been doing some research on what url rewriting is, and from the examples I have come to understand a few things. I am not sure if what I want is url re-writing, per se. Again taking the iisexpress analogy, I know there will be an inbound request uri like:
http://localhost:8082/common/inlcude/style/css.css
But I want this to be actually served by the following uri:
http://testing/common/include/style/css.css
The former uri doesn't exist in the folder which I have virtualized using iisexpress.
Do I need url re-writing here?
Further, in ASP, I have include lines like:
<!-- #include virtual="/common/include/classes/utils.asp" -->
Even these things are supposed to be resolved to their corresponding http://testing/...
counterparts.
ps: I am doing all this is iis 5.1
发布评论
评论(1)
在 ASP.NET 2.0 及更高版本中,您可以使用波形符运算符 (
~
),它用于指定应用程序的根位置。例如:会生成相对 url:
这对于 ASP.NET 页面效果很好。
经典 ASP 和静态 HTML 页面则不同,必须使用以下一种或多种机制:
使一切都相对。如果站点根目录中有页面或 ASP 脚本,请指定
common/include/...
,而不是指定/common/include/...
。如果子文件夹中有页面或 ASP 脚本,则可以通过../common/include/...
(即父路径)引用 CSS。文件夹结构越深,您拥有的../
父路径就越多,因此管理这些相对路径可能会变得混乱。另外,虽然现在并不常见,但一些共享托管服务器不允许父路径。使用包含路径前缀的变量为 CSS 路径添加前缀。例如:
在生产中,您可以将会话值
RootPath
全局设置为/MyApp
,但为了测试,请保留为空字符串。您可以在global.asa
的Session_OnStart
中执行此操作。您还可以使用应用程序范围的值Application("RootPath")
来代替。这仅适用于 ASP 页面。URL 重写 - 如果您有静态 HTML 页面,则 URL 重写可以发挥作用。您可以将开发 PC 上工作的绝对 URL 重写为生产服务器上使用的路径。因此,基本上每次看到
href="/common/...
时,您都会重写为href="/myapp/common/...
。 IIS6 不支持开箱即用的重写,您需要第三方工具,例如 Iconics IRF 或HeliconTech 的 ISAPI_Rewrite3。 IIS 7.x 通过 URLRewrite 模块 2.0 支持 URL 重写。In ASP.NET 2.0 onwards you can use the tilde operator (
~
) which is used to specify where your application is rooted. For example:Would produce a relative url:
This works fine for ASP.NET pages.
Classic ASP and static HTML pages are a different story and one or more of the following mechanisms would have to be used:
Make everything relative. If you have a page or ASP script in the root of the site, instead of specifying
/common/include/...
specifycommon/include/...
. If you have a page or ASP script in a subfolder the you'd reference your CSS by way of../common/include/...
, i.e. parent paths. The deeper the folder structure the more../
parent paths you have so managing these relative paths can get messy. Also, although not common these days, some shared hosted servers disallow parent paths.Prefix your CSS paths with a variable containing a path prefix. For example:
In production you'd globally set the session value
RootPath
to/MyApp
, but for testing leave as an empty string. You could do this inSession_OnStart
in yourglobal.asa
. You could also use an application wide valueApplication("RootPath")
instead. This would only work for ASP pages.URL Rewriting - if you have static HTML pages then URL rewriting can come to the rescue. You would rewrite the absolute url's which work on your dev PC to the path used on the production server. So basically every time you see a
href="/common/...
you'd rewrite tohref="/myapp/common/...
. IIS6 doesn't support rewriting out of the box, you'd need a third party tool such as Iconics IRF or HeliconTech's ISAPI_Rewrite3. IIS 7.x does support URL rewriting through the URLRewrite Module 2.0.