简体   繁体   English

从另一个页面的代码隐藏中获取 ASP.Net 页面的 URL

[英]Get URL of ASP.Net Page in code-behind from another page

I am trying to find away to determine a pages url from the page object.我试图从页面 object 中找出页面 url。 It seems you can only get the path of the current context.看来您只能获取当前上下文的路径。

A page can have several urls.一个页面可以有多个 url。 For example, I have a server running at home and the site's url is different depending on where I check it from.例如,我有一台服务器在家里运行,站点的 url 因我从哪里检查而异。 When I'm at home I just use the internal server name so that the traffic never leaves my home network.当我在家时,我只使用内部服务器名称,这样流量就不会离开我的家庭网络。 When I'm elsewhere I have to use the dyndns.org -based url.当我在其他地方时,我必须使用基于 dyndns.org 的 url。 I could also configure several different sites to all point to the same place.我还可以将几个不同的站点配置为都指向同一个地方。 The point is that this information is not tied to your page's class type, or even a specific instance.关键是此信息与您页面的 class 类型甚至特定实例无关。

Therefore, the URL of a page can only be determined on a per-request basis, and sure enough you can get it by looking at Request.Url所以,一个页面的URL只能根据每个请求来确定,果然看Request.Url就可以得到

Hopefully one of these will help you.希望其中之一对您有所帮助。

Understanding Paths in ASP.NET了解 ASP.NET 中的路径

Expression - Evaluation表达式 - 评估

this.TemplateSourceDirectory - /informit/subdir this.TemplateSourceDirectory - /informit/subdir

Request.MapPath("log.txt") - c:\mywebdirs\informit\subdir\log.txt Request.MapPath("log.txt") - c:\mywebdirs\informit\subdir\log.txt

this.MapPathSecure("log.txt") - c:\mywebdirs\informit\subdir\log.txt this.MapPathSecure("log.txt") - c:\mywebdirs\informit\subdir\log.txt

Request.Path - /informit/subdir/pathsample.aspx/extra Request.Path - /informit/subdir/pathsample.aspx/extra

Request.FilePath - /informit/subdir/pathsample.aspx Request.FilePath - /informit/subdir/pathsample.aspx

Request.CurrentExecutionFilePath - /informit/subdir/pathsample.aspx Request.CurrentExecutionFilePath - /informit/subdir/pathsample.aspx

Request.PathInfo - /extra Request.PathInfo - /额外

Request.PhysicalPath - c:\mywebdirs\informit\subdir\pathsample.aspx Request.PhysicalPath - c:\mywebdirs\informit\subdir\pathsample.aspx

Request.PhysicalApplicationPath - c:\mywebdirs\informit\ Request.PhysicalApplicationPath - c:\mywebdirs\informit\

Request.ApplicationPath - /informit Request.ApplicationPath - /informit

Request.Url - http://localhost/informit/subdir/client.aspx/extra Request.Url - http://localhost/informit/subdir/client.aspx/extra

Request.RawUrl - /informit/subdir/pathsample.aspx/extra Request.RawUrl - /informit/subdir/pathsample.aspx/extra

Response.ApplyAppPathModifier("foo.aspx") - /informit/subdir/foo.aspx Response.ApplyAppPathModifier("foo.aspx") - /informit/subdir/foo.aspx

this.ResolveUrl("~/client.aspx") - /informit/pathsample.aspx this.ResolveUrl("~/client.aspx") - /informit/pathsample.aspx

If the page you are seeking is in the same directory as the current page, or a subdirectory of it, then you can use something like the following (adding the sub-directory page into the string, as needed):如果您要查找的页面与当前页面位于同一目录或其子目录中,则可以使用以下内容(根据需要将子目录页面添加到字符串中):

string targetUrl =  HttpContext.Current.Request.Url.Scheme
                 + "://"
                 + HttpContext.Current.Request.Url.Authority
                 + this.TemplateSourceDirectory + "/Page.aspx";

It should be available via它应该可以通过

string currentUrl = Page.Request.Url.ToString();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM