声明:如非注明原创,则本blog所发日志为引用,向原创作者致意!敬礼!
佛曰: 人生最大的财富是健康
佛曰: 人生最大的幸福是放得下
站点公告
最新日志
博文评论
博客留言
博客登陆
博文搜索
博客信息
收藏连接
 
获取URL,asp, asp.net
janedong 发表于 2006/3/22 10:55:00

A.                 asp

 

 How do I get the name of the current URL / page? 


   http://www.aspfaq.com/2072       created: 2000-08-13        last updated: 2002-08-15 15:51       this article is printer friendly   

This one is pretty easy, but there are two parts. 
 
To retrieve the name of the current file, you can use any of these: 
 

<% 
    Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    Response.Write Request.ServerVariables("PATH_INFO") & "<br>" 
    Response.Write Request.ServerVariables("URL") & "<br>" 
%>

 

E.g., http://localhost , you will get the result: “/Default.asp


To make that path local (for example, to use with FileSystemObject), just apply the server.mappath() method to the result. 
 
To get the entire URL, including the http:// or https:// prefix, you can do this: 
 

<% 
    prot = "http" 
    https = lcase(request.ServerVariables("HTTPS")) 
    if https <> "off" then prot = "https" 
    domainname = Request.ServerVariables("SERVER_NAME") 
    filename = Request.ServerVariables("SCRIPT_NAME") 
    querystring = Request.ServerVariables("QUERY_STRING") 
    response.write prot & "://" & domainname & filename & "?" & querystring 
%>

 
To get the page name ONLY, use something like this: 
 

<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    if instr(scr,"/")>0 then 
        scr = right(scr, len(scr) - instrRev(scr,"/")) 
    end if 
    response.write scr 
%>

 
Or, without the IF logic: 
 

<% 
    scr = Request.ServerVariables("SCRIPT_NAME") & "<br>" 
    loc = instrRev(scr,"/") 
    scr = mid(scr, loc+1, len(scr) - loc) 
    response.write scr 
%>

 
Now. If your file is an #i nclude within another file, the above scripts will produce the name of the CALLING file (since the included file is first integrated into the calling script, then the ASP within it is all executed in the context of the 'parent' file). One way you can work around this is to re-populate a current_filename variable before loading each include file, for example: 
 

<% 
    current_filename = "filetoinclude.asp" 
%> 
<!--#i nclude file='filetoinclude.asp'--> 

 
(And no, don't try passing current_filename as a variable to the #i nclude directive; see Article #2042.) 
 
Then, in filetoinclude.asp: 
 

<% 
    Response.Write "Current file: " & current_filename 
%>

 
Of course, you could just as easily hard-code the filename inside of each include file. But I suppose that solution would somewhat defeat the purpose of retrieving that information at least somewhat dynamically.

 

 

b. asp.net

Top of Form

dudu-快乐程序员

技术改变世界

Request获取url信息的各种方法比较

ASP.NET编程中经常需要用Request获取url的有关信息,Request中有多种方法获取url信息,但我经常忘了各种方法的具体作用,今天我就写了个测试程序,将各种方法得到的结果列出来,以后用时直接参考一下就行了。
测试的url地址是http://www.test.com/testweb/default.aspx, 结果如下:

Request.ApplicationPath:
/testweb
Request.CurrentExecutionFilePath:
/testweb/default.aspx
Request.FilePath:
/testweb/default.aspx
Request.Path: /testweb/default.aspx
Request.PathInfo:
Request.PhysicalApplicationPath:
E:\WWW\testweb\
Request.PhysicalPath: E:\WWW\testweb\default.aspx
Request.RawUrl: /testweb/default.aspx
Request.Url.AbsolutePath: /testweb/default.aspx
Request.Url.AbsoluteUri: htt
p://www.test.com/testweb/default.aspx
Request.Url.Host: www.test.com
Request.Url.LocalPath: /testweb/default.aspx

posted on 2004-02-20 18:17 dudu 阅读(2233) 评论(4)  编辑 收藏 收藏至365Key

Bottom of Form

 

发表评论:

    昵称:
    密码:
    主页:
    标题: