【发布时间】:2013-05-09 10:37:11
【问题描述】:
我知道这很容易,但我忘记了如何以及因为我不记得它叫什么了;尝试寻找解决方案很困难。
我们如何在 C# 中请求页面名称?
例如:
String pageName = String.Empty;
if(IsPost)
{
pageName = Request.PageName; // example only
}
【问题讨论】:
标签: c# asp.net .net razor webmatrix
我知道这很容易,但我忘记了如何以及因为我不记得它叫什么了;尝试寻找解决方案很困难。
我们如何在 C# 中请求页面名称?
例如:
String pageName = String.Empty;
if(IsPost)
{
pageName = Request.PageName; // example only
}
【问题讨论】:
标签: c# asp.net .net razor webmatrix
让答案更清晰
public string GetPageName()
{
string path = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
System.IO.FileInfo info = new System.IO.FileInfo(path);
return info.Name;
}
【讨论】:
string pageName = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Url.AbsoluteUri);
如果您只想要 .aspx 文件名:
pageName = Page.Request.Url.Segments[Page.Request.Url.Segments.Length - 1];
在以下位置有一些使用 URI 类的好示例: http://www.dotnetperls.com/uri
【讨论】:
string pageName = Request.UrlReferrer != null ? System.IO.Path.GetFileName(Request.UrlReferrer.AbsolutePath) : Request.Url.Segments[Request.Url.Segments.Length - 1];
这也可以与Url rewrite 一起使用。
【讨论】: