user1659510
user1659510

Reputation: 283

How to get the name of the previous page from redirected page

I want to get the name of the previous page, but the problem is that my code is unable to get the name of the previous page. For example, I have two pages Previous.aspx and Next.aspx. In Next.aspx page I want the page name of the page URL Previous.aspx. How can I do this? I have tried with the following code in Next.aspx page.

public string GetPreviousPageName()
{
    string Path = System.Web.HttpContext.Current.Request.UrlReferrer.AbsolutePath;
    System.IO.FileInfo Info = new System.IO.FileInfo(Path);
    string pageName = Info.Name;
    return pageName;
}

Upvotes: 0

Views: 7739

Answers (2)

Udara Kasun
Udara Kasun

Reputation: 2216

In Controller

// Home is default controller
      var controller = (Request.UrlReferrer.Segments.Skip(1).Take(1).SingleOrDefault() ?? "Home").Trim('/');
// Index is default action 
      var action = (Request.UrlReferrer.Segments.Skip(2).Take(1).SingleOrDefault() ?? "Index").Trim('/');

In Authorization Class

     var Url= filterContext.HttpContext.Request.UrlReferrer;
// Home is default controller
     var OldcontrollerAuth = (((Url != null) ? Url.Segments.Skip(1).Take(1).SingleOrDefault() : "Home") ?? "Home").Trim('/');
// Index is default action 
     var OldActionAuth = (((Url != null) ? Url.Segments.Skip(2).Take(1).SingleOrDefault() : "/") ?? "/").Trim('/');

Upvotes: 0

Kao
Kao

Reputation: 7364

Here is how you can achive this:

1.You have to store somewhere URL of page you want to recall, for example using ViewState in Page_Load() method:

if( !IsPostBack )
{
     ViewState["ReferrerUrl"]  = Request.UrlReferrer.ToString()
} 

2.Retrieve url from ViewState:

 string referrerUrl = (string) ViewState["ReferrerUrl"];

3.Get name of page with this url:

System.IO.FileInfo fileInfo = new System.IO.FileInfo(referrerUrl);
string pageName = fileInfo.Name;

Upvotes: 3

Related Questions