How to find the url of parent page of a User control - asp.net

I have a user control where if a certain action is performed I want to redirect to the page the user was on with some additional query string parameters.
So, if UserControl.ascx was on Home.aspx, I want to redirect to Home.aspx?action=true, and if UserControl.ascx was on Profile.aspx, I want to redirect to Profile.aspx?action=true
So basically, in my UserControl.ascx.cs I want to get the URL of the Parent Page. How can I get it?

You can look at the Request.Url, Request.RawUrl, Request.FilePath, and some of the other similar properties of the Request object - depending on how you're using this.
This will give you the requested URL from the browser, which will in turn tell you which page your control is living on.

You still have access to the request object from the user control, so do something like this:
string currentUrl = Request.Url.AbsoluteUri.ToString();

Request.UrlReferrer will get you the URL of the previous page... usually. There are some situations where it could be empty:
links clicked from an email message
shortcuts saved to a desktop
spoofed URLs
perhaps some settings or browsers
probably other scenarios as well
As long as your code "plays nicely" when UrlReferrer is empty or invalid, you should be good to go.

Request.Url.Scheme + "://" + Request.Url.Host + Request.RawUrl

Related

Is there a Server.Transfer() alternative to get a user's previous page?

Is there any way to know the previous page a user visited (on my site of course) from server side? I know I can do it if user was redirected before with Transfer() method. Does any history exist during session?
You can use http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx to get previous user client page.
Is the page that you looking for inside your own site? If so, you can do this to enable different reactions for different pages. If it is outside of your site, then I would go with UrlReferrer like Trekstuff mentioned.
If Not PreviousPage Is Nothing Then
Dim str As String = PreviousPage.AppRelativeVirtualPath
If str = "~/(DESIRED URL)" Then
End If
End IF

Spring mvc url issues

I am using Spring mvc and I have this problem:
Every method in my controller returns a name of a jsp, and as for presentation - all good. The problem is with links (hrefs). The window's link does not change and the relative links gets me to unwanted places.
For example (not real):
I have a view which I can access by:/test1/{id}
and an update view which I access by /test1/{id}/update
The post method, saves, and return the first view (getting you back to the viewing screen).
The presentation is OK and I can see the updated data. But the window url was not changes and if I try to update again, I am sent to this location: /test1/{id}/update/update.
How can I solve this?
Short answer: use redirects!
You MUST use redirects while doing something persistant. I guess you are modifying data when POSTing to those url. So you absolutely need to use redirects. This resolves the problem, when user refreshes the page after making an update. Have you ever noticed browser's warnings about resending the data?
Sample code:
#RequestMapping("/test1/{id}/update")
public ModelAndView update(#PathVariable("id") String id){
// make an update
return new ModelAndView("redirect:/test1/" + id);
}
This will send 301 or 302 HTTP status to the user. And the browser will imeddiately follow to the redirect url. Note that you should use not a view name, but url.

HttpApplication and the current page

As we all know the HttpApplication object holds objects regarding the requested page (HttpRequest) and the page where we are supposed to be redirected (HttpResponse); I need to get the url of the page which requested the page; how could I get it from the HttpApplication ?
You want to check the url referrer header like so:
string MyReferrer = Request.UrlReferrer.ToString();
This value is set by the browser however - so can't exactly be trusted from a security viewpoint.
You need to use the HttpRequest.UrlReferrer property see - http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx

Why is Request.QueryString readonly?

I thought you couldn't change the QueryString on the server without a redirect.
But this code works* for me:
Request.QueryString edit
I'm so amazed.
So here are my questions regarding this:
Why is Request.QueryString readonly?
Why does this code/hack work*?
How safe is it, if you change to readonly as soon as you are done editing, both regarding bad errors or unexpected behaviour, and regarding maintaining and understanding the code?
Where in the event cycle would it make most sense to do this crazy edit if you are only using PageLoad and OnPageRender?
*More details:
I have a page with items that are grouped into tabs. Each tab is an asp:LinkButton
I want to be able to link directly to a specific tab. I do that with a QueryString parameter 'tab=tabName'. It works. But when I then click a new tab, the querystring is still in the Url, and thus the tab specified in the Querystring gets activated and not the one I clicked.
By using Request.QueryString edit this does not happen. Then my solution 'works'.
Thanks in advance.
Well the QueryString property is readonly because it cannot be changed on a single request. Obviously the browser sends only one request with only one string so only one collection is created. The hack uses reflection (i.e. manipulates the code and the memory) to change stuff that you cannot change normally. This hack breaks the encapsulation and the design of the QueryString property. You should not use it. It makes no sense from design standpoint. Your query DOES NOT change so why change the object that represents it? Only the browser can send new query string so you are basically lying to your own code about what the browser sent.
If you want the tabs to use the URL just use Hyperlinks instead of LinkButton.
From what I remember reading, this is a security standard that all browsers adhere to. It's main purpose is to stop phishing attacks, where someone could have the website www.MyLameWarcraftPhishingSite.com" and when someone hits the page, rewrite the url to look like www.blizzard.com. The only way to get to that url is to actually redirect to it.
mmm, last post was in Feb 11 - hope its ok to post in this.

asp.net After a Server.Transfer how do you get the path of the current page?

To get the url of the current page, I usually do something like this:
string path = Request.Path;
If I do this after a Server.Transfer then I get the path of the page where the transfer was done. How can I get it for the current page?
For example:
On Page1.aspx I do Server.Transfer ("Page2.aspx")
On Page2.aspx Request.Path returns /Page1.aspx and not /Page2.aspx
I would like to get /Page2.aspx. How can I get it?
You're looking for the Request.CurrentExecutionFilePath property.
AppRelativeVirtualPath (http://msdn.microsoft.com/en-us/library/system.web.ui.templatecontrol.apprelativevirtualpath.aspx) seems to do the trick.
When you use Server.Transfer, despite the URL on browser doesn't change, the Request references URL does. So, you may use the UrlReferrer property's Request.
UrlReferrer gets information about the URL of the client's previous request that linked to the current URL.
For instance
string myContextBrowserPath = Context.Request.UrlReferrer.AbsolutePath

Resources