【问题标题】:Getting name of Page being requested/was requested [duplicate]获取被请求/被请求的页面的名称[重复]
【发布时间】:2013-05-09 10:37:11
【问题描述】:

我知道这很容易,但我忘记了如何以及因为我不记得它叫什么了;尝试寻找解决方案很困难。

我们如何在 C# 中请求页面名称?

例如:

String pageName = String.Empty;
if(IsPost)
{
   pageName = Request.PageName; // example only
}

【问题讨论】:

    标签: c# asp.net .net razor webmatrix


    【解决方案1】:

    让答案更清晰

    public string GetPageName() 
    { 
        string path = System.Web.HttpContext.Current.Request.Url.AbsolutePath; 
        System.IO.FileInfo info = new System.IO.FileInfo(path); 
        return info.Name; 
    }
    

    【讨论】:

    • 谢谢@Ashley!上面的代码给出了完整的文件路径,我稍微扩展了一下,所以它只给出了没有扩展名的文件名:string pageName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Url.AbsoluteUri);
    【解决方案2】:

    如果您只想要 .aspx 文件名:

    pageName = Page.Request.Url.Segments[Page.Request.Url.Segments.Length - 1];
    

    在以下位置有一些使用 URI 类的好示例: http://www.dotnetperls.com/uri

    【讨论】:

      【解决方案3】:
      string pageName = Request.UrlReferrer != null ? System.IO.Path.GetFileName(Request.UrlReferrer.AbsolutePath) : Request.Url.Segments[Request.Url.Segments.Length - 1];
      

      这也可以与Url rewrite 一起使用。

      【讨论】:

      • 我相信如果没有 URLReferer 东西,这将一直有效:pageName = new System.Text.RegularExpressions.Regex("_(.*?)_aspx").Match(this.Page.ToString( )).Groups[1].Value;
      猜你喜欢
      • 2018-11-09
      • 2020-07-18
      • 1970-01-01
      • 2021-01-11
      • 2011-04-23
      • 2011-01-28
      • 1970-01-01
      • 2014-05-26
      相关资源
      最近更新 更多