开发者

asp.net how to check if the user is on a particular page?

i want to add code to check if a user is on default.aspx in the master page _load event.

how can i check in the master page i开发者_StackOverflow社区f the page being requested is default.aspx?


You should check Request.Url.LocalPath which should either be "/default.aspx" or "/".

if (string.Compare(Request.Url.LocalPath,"/default.aspx") == 0 || string.Compare(Request.Url.LocalPath,"/") == 0)
{
    // your code
}


You should be able to test type of Page property in Master. eg:

public partial class DefaultMaster : System.Web.UI.MasterPage
{

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        if(this.Page is DefaultPage) {
            ...
        }
    }

}


You're probably looking for

Request.Url


string currentUrl = HttpContext.Current.Request.Url.LocalPath;

if(currentUrl.EndsWith("default.aspx") || currentUrl.EndsWith("/"))
{
    DoSomething();
}


I'll use

if (Request.AppRelativeCurrentExecutionFilePath == "~/"
    || string.Equals(Request.AppRelativeCurrentExecutionFilePath, "~/default.aspx", StringComparison.CurrentCultureIgnoreCase))
{
    // ....
}

Request.AppRelativeCurrentExecutionFilePath will ignore your localhost, local host file mapping, or virtual directory name and return the file path relative to your web site, while Request.Url.LocalPath still includes virtual directory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜