Get Original Url After HttpContext.RewritePath() Has Been Called
Mar 11, 2010
I am working on a web app which makes use of a 3rd party HttpModule that performs url rewriting. I want to know if there's any way to determine the original url later on in Application_BeginRequest event. For example...
Original url: [URL] Re-written url (from 3rd party httpmodule): [URL] In the past I have written HttpModules that store the original url in HttpContext.Items but, this is a 3rd party app and I have no way of doing that.
I'm developing a custom URL Rewriter for a ASP.Net 3.5 project. This rewriter is not functionally different than most rewriters out there, the only difference being that the friendly URL collection is not loaded from a web.config file -- it's coming from a database instead. I made the naive assumption that it would be easy to develop a custom rewriter module from scratch, but now I know the mess I put myself in. I digress; let's go straight to the technical issues.
While testing the rewriter, I set up a friendly url that would take the user to a web form. Postbacks from this form should not alter the friendly address, as anyone would expect, sohttp://my.web.site/app_root/FriendlyURL is always rewritten asttp://my.web.site/app_root/not_friendly/form.aspxThings are fine when the browser first loads the FriendlyURL. The page comes up and is completely functional. However, when the form is posted back to the server, the page simply reloads but, at server-side, IsPostBack is false. It's like F5 was hit, except that a HTTP POST had indeed occurred.Unsurprisingly, when the interaction occurs through the "unfriendly" URL, the POST action triggers the postback as expected. This evidence suggests that HttpContext.RewritePath is somehow messing with the page lifecycle in a way that it loses sense of the postback operation.
I have various web pages that need to build up a URL to display or place it in an emitted email message. The code I inherited had this value for the name of the webserver in a Public Const in a Public Class called FixedConstants. For example:
Public Const cdServerName As String = "WEBSERVERNAME"
Trying to improve on this, I wrote this:
Public Class UIFunction Public Shared myhttpcontext As HttpContext Public Shared Function cdWebServer() As String Dim s As New StringBuilder("http://") Dim h As String h = String.Empty Try h = Current.Request.ServerVariables("REMOTE_HOST").ToString() Catch ex As Exception Dim m As String m = ex.Message.ToString() 'Ignore this should-not-occur thingy End Try If h = String.Empty Then h = "SomeWebServer" End If s.Append(h) s.Append("/") Return s.ToString() End Function
I've tried different things while debugging such as HttpContext.Current.Request.UserHostName and I always get an empty string which pumps out my default string "SomeWebServer". I know Request.UserHostName or Request.ServerVariables("REMOTE_HOST") works when invoked from a page but why does this return empty when invoked from a called method of a class file (i.e. UIFunction.vb)?
We're migrating an application to use IIS7 integrated mode. In library code that is designed to work either within the context of an HTTP request or not, we commonly have code like this:
if (HttpContext.Current != null && HttpContext.Current.Request != null) { // do something with HttpContext.Current.Request } else { // do equivalent thing without HttpContext.. }
But in IIS7 integrated mode the check for HttpContext.Current.Request throws an exception whenever this code is called from Application_Start.
System.Web.HttpException: Request is not available in this context
How can I detect whether the request is really available without wrapping these calls in an exception handler and taking action based on whether an exception is generated or not.
Looking at HttpContext in Reflector I see it has an internal bool HideRequestResponse field but it's internal so I can only get to it with reflection and that's fragile. Is there a more official/approved way to determine if it's ok to call HttpContext.Request?
This blog post about the subject says not to use HttpContext, but how, in generic library code, can you determine if it's ok to use HttpContext?
I'm using the work-around mentioned there which is to use Application_BeginRequest and an initialized field to only initialize once as part of BeginRequest, but that has to be done in every calling application whereas I'd prefer to make the library code more robust and handle this situation regardless of where it's called from.
Is it possible to do RewritePath in the pages load-event? I call the method, but nothing happens.. The original path is served.. The method fires, coz when I rewrite the path to something that gives an error, the error is fired
I've been working on this problem for some time without success. We have this website that has been working for many years now and we have been using cookieless sessions since the begining. Now we are integrating with another site to handle payment throught redirection. This provider ask for two adresses for success or failure of a payment so we have something like [URL] and [URL]. For design purpose thoses two pages do not exists and I use an httpModule to route to the proper page. I use httpApplication.Context.RewritePath to send to the proper page but I can never find back the session my user had before redirecting to the payment provider.
If I change the session mecanism to use cookies (cookieless="false" or cookieless="UseUri") with a few other tweaks, everything works fine Does anybody know if this has a chance to work? Is there a way to re-attach to an existing session?
I have a problem to rewrite url using RewritePath method.
Code implementation as below.
Class
[Code]....
Page code
[Code]....
Web.config file setting
[Code]....
Now, when I call URL like http://localhost:1404/SampleSite/test then its working and rewrite to the rewrite.aspx page. but when I click on button event then URL is changed to original path like as http://localhost:1404/SampleSite/rewrite.aspx
On button click event, page is just post back. whenever page is post back, URL is changed to its original page. I want URL as it is calling like as http://localhost:1404/SampleSite/test
I have been experimenting with code that will clear all of the cookies in an HttpContext.Response.Initially, I used this:
DateTime cookieExpires = DateTime.Now.AddDays(-1); for (int i = 0; i < HttpContext.Request.Cookies.Count; i++) { HttpContext.Response.Cookies.Add( new HttpCookie(HttpContext.Request.Cookies[i].Name, null) { Expires = cookieExpires }); }
this will error with an OutOfMemoryException because the for loop never exits - each time you add a cookie to the Response, it also gets added to the `Request.
i'm using an example in which i can fake the session.It's for use in a unittest of a mvc controller.In the test i create a controller and then i do this:
FakeHttpContext httpctx = new FakeHttpContext(null,null,null,null,mSessionItems ); ControllerContext ctx2 = new ControllerContext(httpctx,new RouteData(), target);
here mSessionItems is my session and target is my instance of a controller,and indeed, when i'm in a controller reading this.ControllerContext.HttpContext.Session, i have a session, great!but.... i also read the session outside the controller, and there i use HttpContext.Current.Session, and that is null (or actualy, the HttpContext.Current is null).
I am using the ToolkitScriptManager from the Ajax tool kit and I am having a problem finding my button's ID.My ID's on my controls come back with $ symbols instead of _ symbols, like the following:causes problems when attempting to find the control from the Page.Request.Form keys. I cannot seem to find the determining factor that would cause this.Now, I know this is the name and in my source I see that the ID is with the _, so why is the Page.Request.Form showing up with the $ symbol instead? Anybody encounter this before?
I have a textbox and a button. On page load I select one column from one row and put its value in the textbox. I have a button click method that updates the same column/row with the new value in the same textbox.
The problem i'm having is that when I clear the text in the text box, type in new data and hit submit the new text value is not being saved, it uses the old one.
I put a breakpoint at the end of my button click method and it appears that asp.net is sending the old value of the textbox rather than the new one I put in. I'm totally stumped.
I have a page where I have a button and a read only text box, and the button uses javascript to open a popup window with a date picker on it, which is used to set the text box. Here is my button code:
In answering another persons question here on SO, I discovered that there is a small "bug" in my global redirect code.I have wired up a Global class to an HttpModule. It's job is to detect "http:/www." in the URL and redirect the user to the NON www. version
Protected Sub OnBeginRequest(ByVal sender As Object, ByVal e As EventArgs) 'Force Removal of WWW Dim application As HttpApplication = TryCast(sender, HttpApplication) [code]....
when it redirect a page http://www.example.com/AboutUs, the goal is to have it go to http://example.com/AboutUs (the rewritten page) but instead it's going to http://example.com/Default.aspx?Slug=AboutUs (the original page).I tried doing a bit of a hack by changing
Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme)) application.Context.Response.Status = "301 Moved Permanently" application.Context.Response.AddHeader("Location", newUrl.Replace("Default.aspx", "")) to
Dim newUrl As [String] = UrlRegex.Replace(url.ToString(), [String].Format("{0}://", url.Scheme)) newUrl = newUrl.Replace("Default.aspx?Slug=", "") newUrl = newUrl.Replace("Default.aspx", "") application.Context.Response.Status = "301 Moved Permanently" application.Context.Response.AddHeader("Location", newUrl)
not something I want to do anyways since it's a hack, but it didn't work anyways.
The things I need to change are in my default.aspx file. First, at the bottom of my pages it has the name of the company that created the site for us. I want that taken off our site. I know it is a matter of going into the default.aspx file and removing the wording, but I cannot figure out how to go into it and change it.
Also in our default.aspx file, on line 167 and 168 there is a reference to the slideshow that is on our front page. There is a string missing that should point to our last two images, but again, I am unable to get into the default.aspx to edit it.
Evidently when our site was created, the developers used the precompilation tool so now I am unable to edit any of the files when I try to upload them into visual studio. Whenever I try to uplaod them I get the error "This is a marker file generated by the precompilation tool, and should not be deleted!"
I do have all of the original source files for the site, but as you know there are alot of them and I am not sure where to find the original default.aspx file or even what it would be named so I can make the changes to it. Also, once the changes are made will I have to precompile the entire site again, or can I just do the default.aspx and uplaod it to the server?
I need a technique to hide the real original url when I click a link
I mean for i.e. [URL]
I need it like this [URL] in the address bar
That's to hide the physical folder structure
and also if there is some parameters like this: [URL]
I need it to be also the main page like this : [URL]
Over all I need the user see only the address of the main page.
I did my best to solve this by trying the following:
Using iframe with "0" border but there is a problem with the height, the frame trim the bottom of the pageUsing ReWrite URL technique but it needs me to write all the physical paths and the replacement of each one, and you know that there are 10s or 100s of links in MOH website.
I need a dynamic technique that take in consideration passing the parameters between pages and post back event also.
I just wrote an application level err handler for my website. The errors are saved in sql server. Now I download the info back into a datagrid and on the selectedrow changed I put the stacktrace and other big fields in textboxes like so :
[Code]....
However the text is now formatted like this :
txtSearchClient = ""
instead of :
txtSearchClient = "";
I've confirmed that the text is in the right format in SQL server but I cna't find a way to deencode it.
Aso.net.3.5 having ajax(updatePanel), jquery On postback I kip on getting the original value before the control has being change... The div.innerText is the original one.
So most of the time my stylesheets appear properly. The standard/original one always works flawlessly, however it seems sometimes the mobile one is disregarded when looked at from a mobile device
I'm using a Droid X to view the page, in portrait mode, so the device width shouldn't be exceeding the max-width specified above, but sometimes, randomly, it still reverts back to the original css page.
I'm using return url to restore a users site position after a session times out. This works fine. However if the user happens to have a popup open when a session times out, it returns the user to the popup screen (correctly) but the popup now takes up the entire screen. Is there a way to return the popup in it's original size or another way to check for popups, perhaps using the sitemap?
I change the value of the boolean variable to true but then I have to refresh the page by clicking another button or two. Once the ASP.NET page refreshes, the value returns back to false when I want it to be true. How can I prevent it from changing the boolean variable to the original value and keep it as true. What would be the C# syntax for this.
public partial class _Default : System.Web.UI.Page { bool plus = false; bool minus = false; bool multiply = false; [code]...
I have an .aspx page as my custom 404 page on a site set up on IIS 7. I need to retrieve the original URL that the user was trying to access in order to do some processing on the 404 page. The trick is that I need to specifically handle 404's that do not contain a .aspx extension (e.g http://mysite/testurl), which do not get routed through ASP.NET's custom errors section. I can configure IIS to point to my custom 404, but at that point I do not know how to get my original URL?