Url Rewriting in asp.net but maintaining the original url - asp.net

Page aspxHandler = (Page)PageParser.GetCompiledPageInstance(virtualPath, context.Server.MapPath(virtualPath), context);
aspxHandler.PreRenderComplete += AspxPage_PreRenderComplete;
aspxHandler.ProcessRequest(context);
When you call Page.Request.Url after this, you get the Url of the page you rewrote to
...what I'm looking for is to do a rewrite, but for Page.Request.Url to remain as the original url that was passed in. Is that possible?

I had a similar problem using rewriting rules in the web.config. Not sure if this will solve your problem too, but I found that when the url was rewritten, the originally requested URL was accessible through the "HTTP_X_ORIGINAL_URL" server variable.
VB:
string pathAndQuery = Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL") ? Request.ServerVariables("HTTP_X_ORIGINAL_URL") : Request.Url.PathAndQuery
c#:
string pathAndQuery = Request.ServerVariables.AllKeys.Contains("HTTP_X_ORIGINAL_URL") ? Request.ServerVariables["HTTP_X_ORIGINAL_URL"] : Request.Url.PathAndQuery;
That should get you the original path and querystring of the request before rewriting, whether or not rewriting has taken place.

Related

how to forward a link with URI to another link with the same URI

My program directs users to a webpage with their username and password. E.g.
http://example.html?username=username&password=password.
Now I created another page in asp.net and I want some code on example.html to redirect the link to http://example.aspx?username=username&password=password.
So what i want is to get the URI from the first url and direct it to the new url by appending the URI.
Any suggestions?
You can grab the querystring in its entirety via
window.location.search
See this with more about that. Using this, you can extract the parameters, append them to a new URL, and render the link, or set
window.location = "example.aspx" + window.location.search
I believe search comes with "?", but I could be wrong. I assume this is an exmaple; note it's not a good practice to pass the password through a querystring as clear text, especially if you are not using HTTPS. It's generally advisable to do a POST operation, not a GET operation with querystring, when it comes to sensitive information.
Use window.location.search to get everything after "?". Then you can just direct the new aspx page using this java script line.
window.location = "example.aspx" + window.location.search

Having two ? in a URL - Is it valid?

Here is the thing:
Let's say I have a base URL like this:
www.mysite.com?my_first_param=1&my_second_param=2
But now I want to add a new parameter to my URL:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com
Imagine that I am using a service where they add new tracking parameters to my URL. For example:
first_tracking=value1&second_value=value2
Since my BASE url already has a "?" the parameters are added with a "&", so my final URL would look like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com&first_tracking=value1&second_value=value2
This is a correct URL with several parameters, but when I do the redirect to www.myothersite.com, since the parameters start with a "&", they get lost. Would it be correct to add the tracking parameters with a starting "?" ? Like this:
www.mysite.com?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com?first_tracking=value1&second_value=value2
If not, what would be a good approach for dealing with this? I believe is responsibility of the redirect to pass through the tracking parameters to the redirect URL.
You should URLencode each param name and value properly, so if the map of params you want is this:
my_first_param => 1
my_second_param => 2
my_redirect_site => www.myothersite.com?first_tracking=value1&second_value=value2
then you should pass this as the query string:
?my_first_param=1&my_second_param=2&my_redirect_site=www.myothersite.com%3Ffirst_tracking%3Dvalue1%26second_value%3Dvalue2
You should be using a library which does this already to build up URIs to handle this encoding for you.
No, you can't have a second question mark in a URL.
Furthermore, if you have ampersands in the redirect URL, they will be seen as separate parameters for the main URL, and not seen as connected to the redirect URL.
If you want to do a redirect like this, you need to URLEncode the whole of the redirected URL. There are standard functions in most web-facing languages to do this.
Encode the parameter, "?" would be replaced by %3F
It depends on the amount of control you have over the service adding the tracking parameters.
Can you change the url after the parameters have been added?
If you can, then you should use a url builder to add the tracking parameters to your redirect url, then url encode that url entriely, tracking parameters included.
If you are not in control and a third party modifies your url, then you will have to do this when you redirect, read the parameters in the url, take your redirect url and the tracking parameters, add the tracking parameters to the redirect url before redirect.

Getting Filename from virtual URL?

I have a URL like "http://www.ti.com/lit/gpn/TPS767D318-Q1"
which is a path eventually being routed to "http://www.ti.com/lit/ds/symlink/tps767d318-q1.pdf" on the browser(rendering a pdf file). I am processing this URL in a Console application in order to fetch the "pdf" filename that you see in the second URL i provided.
I checked the UriResponse.Absoluteuri property in the httpresponse object and it says "http://focus.ti.com/general/docs/lit/getliterature.tsp?genericPartNumber=TPS767D318-Q1&fileType=pdf"
looks like this is a nested virtual path. Can anybody help on where i can get to the end URL to extract the pdf file name? i did not find it anywhere in the response object. I also checked in the Response headers and nothing there either.
Any help will be appreciated...Thanks
Not sure about ASP, but at the protocol level the initial request may cause a Redirect to be issued by the application/server on the other end, so you can look at the initial HTTP response and check if it's a redirect code, 301, 302 etc. If so, you can follow the 302s until you hit a 200, and that's the final URL you can use to check the filename.
Look at the Content-Disposition header, it might look something like: Content-Disposition: attachment; filename=tps767d318-q1.pdf. This is a common technique for webservices that fetch and "download" files from database, network shares, etc.
It turns out that the URL in my question is actually returning HTML content and doing a "meta tag" redirect. So I had to do the following:
var redirect = Regex.Match(new string(buffer, 0, count), #"\<meta(?=[^>]*http-equiv\W*refresh)[^>]*?content\s*\=[^=>]*url\s*\=\s*(?<Url>[^'"">]+)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
if (redirect.Success)
{
Uri uri = new Uri(new Uri(externalUrl, UriKind.Absolute), new Uri(redirect.Groups["Url"].Value, UriKind.RelativeOrAbsolute));
return SaveUrlToTemporaryFile(uri.AbsoluteUri, needsFullDownload);
}
I'm getting the final URL out of the meta tags from the returned HTML content, and calling my download routine again.

Url rewriting, raw url is omitting special characters

I am creating a site in asp.net with URL rewriting.
My initial url is like
/mypage/languagename/ASP.NET
it is working fine when I am excepting taking the language name with
HttpApplication app = (HttpApplication)sender;
app.Request.RawUrl // this is giving me ASP.NET
but when the initial URL is
/mypage/languagename/C#
I am getting only C from the rawURL instead of C#.
How can I get the same?
Use UrlDecoder becasue # is URL Encoded Character
You need to encode that url because it contains html special character, ie the #
Check this class, System.Web.HttpServerUtility. Use that class UrlEncode method to encode the url before using and it will solve your problem.

Problem with slash within a query string

I'm using the WebRequest class to make a request to some site. The query string contains a slash (/), which cause to the url to be cut by the site, because it doesn't see it as part of the query string.
The query string is: "my params / separated by slash".
The request:
var request = WebRequest.Create(
"http://www.somesime.com/q-my+params+%2f+separated+by+slash"
);
What I missing?
EDIT:
After all answers here are update:
I was wrong about query string, it's not actually query string, but the url should look (without "?"):
"http://www.somesime.com/q-my+params+%2f+separated+by+slash"
The url "http://www.somesime.com/q-my+params+%2f+separated+by+slash" is result of Server.UrlEncode method. The code:
var url = "http://www.somesime.com/q-" +
Server.UrlEncode(#"my params / separated by slash");
EDIT 2:
If I place the resulting url into a browser, everything works.
But if I run it through WebRequest class, the url results as it was called without "/ separated by slash" part
If this is your actual code you are missing the ?:
var request = WebRequest.Create("http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
you forgot to put "?" before key name , so try :
var request = WebRequest.Create("http://www.somesime.com?q=my+params+%2f+separated+by+slash");
You need to have a look at apaches AllowEncodedSlashes option
http://httpd.apache.org/docs/2.0/mod/core.html#allowencodedslashes
You should be able to enable this through .htaccess or httpd_conf
UrlEncode it. (You will need a reference to System.Web )
string url = "http://www.somesime.com/?q=my+params+%2f+separated+by+slash");
var request = WebRequest.Create(HttpUtility.UrlEncode(url));
This part of the URL:
/q=my+params+%2f+separated+by+slash
is actually a continuation of the URL, the website probably uses some kind of URL routing. Query strings are denoted by the '?' and seperated by '&'.
If you did need to remove '/' from a URL then HttpUtility.UrlEncode would be the way to go, but this will not benefit you in your case as any encoding done to the URL will almost definitely cause your WebRequest to fail.
?
(Yes, that is what you are missing. :)
Use like this
$qrypic = 'INSERT INTO tbl_propics (userID,num,imagename,propic) VALUES ("$id","1","http://\graph.facebook.com/\$id/\picture?type=large","1")';

Resources