Is there some method in asp.net for getting an absolute url with cookieless session?
UPDATE: I need create other new URL. It is not requested URL.
I´m using Response.ApplyAppPathModifier for getting relative URL with cookie session.
Thx in advance,
I tried Request.RawUrl and Request.Url (and its properties in the Immediate window).
None did show the extra attribute the setting (web.config):
<sesionState cookieless="true" />
makes in the url.
Example
http://localhost:2677/WebSite1/(S(3abhbgwjg33aqrt3uat2kh4d))/cookielesssessiondetection.aspx
However if you're after that part, 3abhbgwjg33aqrt3uat2kh4d, you can get it via:
Session.SessionID
Update after the updated question:
I put in my test application a Hyperlink control on the page. In code behind, Page_Load, I added:
HyperLink1.NavigateUrl = Response.ApplyAppPathModifier("About.aspx");
When I run that page then the url to About.aspx gets set with the cookieless session part included.
When I check the source of the rendered html in my browser I see this:
<a id="HyperLink1" href="/WebSite1/(S(3tzgdnmhwxmxqer10d11auuq))/About.aspx">HyperLink</a>
Did you try Request.Url.ToString(). It should work for you.
If you needed was the url of another page on your site then you can proceed like this...
String url = new Uri(Context.Request.Url, ResolveUrl("~/ABC.aspx")).ToString )
We also have something like Request.Url.AbsoluteUri
I hope One of the above should work for you.
Related
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
I have a page where I set the PostBackUrl property on a login button. I am using the IIS7 Url Rewriting Module. I am also rewriting the Page.Form.Action attribute to match the rewritten url, so that postbacks work properly. When I have the PostBackUrl property set, the page adds a "__PREVIOUSPAGE" hidden input. The problem is that ALL postbacks are now broken, such that the request is seen as a new request, and Page.IsPostback == false. Removing the line of code that sets PostBackUrl fixes all my postback issues.
I don't think I can rewrite the encrypted __PREVIOUSPAGE value to match the current rewritten url, but I suspect that is the issue. Has anyone else encountered and solved this problem?
ASP.NET webforms and url rewriting have never played particularly well. You might want to look at routing, which will work loads better . . .
I have a page on my site, for purposes of this example lets say http://myhost/Pages/Default.aspx. On Default.aspx I have a user control called MyControl.ascx which lives in /Controls/MyControl.ascx. So the tree looks something like this
Root
Pages
Default.aspx
Controls
MyControl.ascx
Whenever I place a HyperLink control on MyControl.ascx and specify a NavigateUrl, this path is relative to the control, not the URL of the page. So for instance if in NavigateUrl I specified "AboutMe.aspx", the URL would be rendered as http://myhost/Controls/AboutMe.aspx instead of http://myhost/Pages/AboutMe.aspx. Is there any way I can make this relative to the page URL? I've tried the solution here: Relative path from an ASP.NET user control NavigateUrl but it didn't work for me.
Edit
To clarify, I'd like this to be generic enough so that if I didn't know what the path was the solution would work. So I don't really want to harcode "~/Pages/Default.aspx" in the NavigateUrl
You have a bunch of options:
You could hardcode the Url using the ~ operator which give you the root and then define it from there like: ~/Pages/AboutMe.aspx. Keep in mind that the ~ operator is recognized only for server controls and in server code.
You could also use the .. to get you to where you want as it will navigate back up the folder structure like: ../Pages/AboutMe.aspx
You could create a helper methods to return you a valid root to your Pages folder, Images, Javascript, etc...
The HttpRequest.ApplicationPath will get your the virtual application's root path on the server which you can use to build off.
For more information on Pathing options you should read this article on MSDN:
ASP.NET Web Project Paths
EDIT: Based on your edit to your question, you should then pass in the relative URL into you user control via a property. Let the page that uses the control define the relative path to the resource it will require.
The best way to achieve this is to use
string.Format("{0}", Page.Request.Url.PathAndQuery);
in NavigateUrl property
Just in case anyone else stumbles across this as I did...
In the code behind, I set the NavigateUrl to:
link.NavigateUrl = Page.ResolveUrl("aboutus.aspx");
How do you set the NavigateUrl of the hyperlink? If I set it using the ~ character, then it works as expected:
MyControl.ascx
<asp:HyperLink runat="server" NavigateUrl="~/Pages/Default.aspx" ... >
Simpler than that. Just change the usercontrol's AppRelativeTemplateSourceDirectory property to the value of the same property of the parent Page.
protected override void OnInit(EventArgs e)
{
AppRelativeTemplateSourceDirectory = Page.AppRelativeTemplateSourceDirectory;
base.OnInit(e);
}
then all relative path in user control will be if there was in 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
on our site we do url rewriting to generate massive amounts of database generated pages. on every page, there is a Login control for users. like this:
Internal aspx page: /DB.aspx?id=123
User visible url: /ABC/123.aspx, /ABC/456.aspx ... (url rewritten)
unfortunately, the tag on each page has an action attribute of "DB.aspx?id=123". when the user clicks the button the browser is posting to /ABC/DB.aspx?id=123 which of course does not exist.
solutions i tried:
1. change the action attribute by subclassing HtmlForm. this destroys the all other forms on the site.
2. remove the action attribute (so that the browser is always posting to the same url). this works on the rewritten pages but on "/" (the default.aspx in the root dir) i get a message that the verb post is not allowed on "/" (iis 6 and i have no control over mappings)
anybody?
Check this really nice blog post from scott gu, http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx.
"Specifically, you can take advantage of the new ASP.NET 2.0 Control Adapter extensibility architecture to customize the rendering of the control, and override its "action" attribute value with a value you provide. This doesn't require you to change any code in your .aspx pages"
Check the section: "Handling ASP.NET PostBacks with URL Rewriting", I have used the adapter he posted successfully.
Ps. be aware there are some issues on asp.net when using url rewrite when using cookieless session, and the rewritten url is deeper than the original page, just like the one you have. (/abc/apage vs. /db?). The issue is right into the source code of the framework, there are workarounds but that's a whole subject (with tradeoffs :( ... you might want to have them at the same level).
Semantics maybe, but does the action attribute = "DB.aspx?id=123" or "/DB.aspx?id=123"? Assuming your URL rewriting allows pass-through to physical pages, this might be your issue.
I never did it, but I saw the code using Reflector and I guess you can fix it this way:
On the page:
this.Form.Action = null;
or:
this.Form.SetAttribute("action", null);
If that doesn't work, just set the path you want:
this.Form.SetAttribute("action", "ABC/123.aspx");
If you upgrade to ASP.NET 3.5 SP1, the action property is now properly recognized and can be set from codebehind.