Url rewriting, raw url is omitting special characters - asp.net

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.

Related

Querystring in C# web application

I have created web application.I am giving one of web page to client as api.Client can pass parameter to web page like below
Ex: www.domainname.com/Testpage.aspx?name=pinky&city=pune&number=xxxxxxxx
In same page Testpage.aspx,I am accessing/fetching querystring like below.
string s= Request.Querystring["name"];
I am not sure how client can call api.I mean to say from browser or code throught.Whether client use urlencode or not?
from code
www.domainname.com/Testpage.aspx?name=Server.UrlEncode("pinky")&city=Server.UrlEncode("pune")&number=Server.UrlEncode("xxxxxxxx")
will below code work ? or does i need to decode?If client did not use Encode then decode work fine?.I want user querystring value further processing and insert into table.
string s= Request.Querystring["name"];
You need not decode it. If they have entered special characters and not encoded then it will not reach your server-side code at all because it will throw a bad request error. If they have encoded at their end then it will be automatically decoded at your end.
Even If they have not encoded, Your decode will work fine.

Decode and RequestQueryString

My URL is
www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b
I would love to read the QueryString value of t. So i can parse it as GUID.
Unfortunatelly that & thing, seems to mess things up.
How can i parse the value of the t Query string?
My URL is
I hope you realize that this is not a valid URL. This is a HTML encoded string. Do not confuse with an URL. URLs should be properly URL encoded, not HTML encoded. And since you start with something invalid your only chance is to use some ugly string parsing/regex to extract the necessary information. Since this looks like an HTML encoded string you could HTML decode it first:
var myUrl = "http://www.domainname.com/default.aspx?l=en&t=32600483-1618-4f09-9a86-c12de4dafc7b";
myUrl = HttpUtility.HtmlDecode(myUrl);
var values = HttpUtility.ParseQueryString(myUrl);
var t = values["t"];
But I repeat once again: don't do this: tackle the problem at its root. And the root of your problem is the origin of this URL. So if you have control over the generation of this URL then fix it so that you can have a valid URL that you could easily work with with the built-in methods. If you don't have control over the generation of the URL then notify the author of the code that he has a bug in it and ask him to fix it because he has provided you a non-properly encoded URL and you are obliged to use some ugly string parsing mechanisms to extract the information from it.

Problem in URL validation using ASP.Net Regular Expression Validator

I'm trying to use the ASP.Net Regular Expression Validator to validate a URL field. URL is www.tachibana.co.jp/tokyosys.htm. Validation expression used is ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" but this is not working. Is there anything wrong with the Regular expression or URL ?
Rules are as below.
It should validate even if (http or
https) is included or not.
It should also trim the URL before
validating.
It should also validate the sub
domain URL's
It should also validate the URL's to
a file on domain or sub domain.
thanks
The problem is that your regex
http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?
expects the URL to start with http:// or https://. Also, the dash inside the character class is misplaced.
Edit: Now that you've posted your rules, I suggest this:
^\s*((?:https?://)?(?:[\w-]+\.)+[\w-]+)(/[\w ./?%&=-]*)?\s*$
After a successful match, group 1 will contain the domain, and group 2 will contain the file path, if present.
^(?i)(http|ftp|https)\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$
"(http(s)?://)?([\www]+\.)+[\w-]+(/[\w- ;,./?%&=]*)?"
var re = /(http(s)?:\\)?([\w-]+\.)+[\w-]+[.com|.in|.org]+(\[\?%&=]*)?/
if (re.test(txt)) {
alert('Valid URL')
}
you can add domain needed in the last field of com,in,org

Url Rewriting in asp.net but maintaining the original url

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.

ASP.NET: Get *real* raw URL

In ASP.NET, is there any way to get the real raw URL?
For example, if a user browse to "http://example.com/mypage.aspx/%2F", I would like to be able to get "http://example.com/mypage.aspx/%2F" rather than "http://example.com/mypage.aspx//".
I would of course like a clean way to do it, but I can live with a hacky approach using reflection or accessing obscure properties.
At the moment, I try to use the uri in the Authorization-header (which works), but I cannot rely on that always being there.
EDIT:
What I really want to do is to be able to distinguish between "http://example.com/mypage.aspx/%2F" and "http://example.com/mypage.aspx/%2F%2F".
It looks like ASP.NET first converts "%2F%2F" into "//" and then converts the slashes into a single slash.
So just re-encoding it is not going to work.
I wasn't able to test this because it only works in IIS and not the ASP.NET Development Server that is part of Visual Studio, but try:
Request.ServerVariables[ "HTTP_URL" ]
The following code works for me:
IServiceProvider serviceProvider = (IServiceProvider)HttpContext.Current;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)serviceProvider.GetService(typeof(HttpWorkerRequest));
string realUrl = workerRequest.GetServerVariable("HTTP_URL");
Note that this only works when running on the IIS and not under f.x. ASP.NET Development Server!
Thanks to Lucero for the answer in another thread and Zhaph for pointing me to the thread.
See also:
Get the exact url the user typed into the browser
Server.HtmlEncode(Request.RawUrl);
The raw URL is defined as the part of the URL following the domain information. In the URL string http://www.contoso.com/articles/recent.aspx, the raw URL is /articles/recent.aspx. The raw URL includes the query string, if present.
see also:link text
I can't test here, but this might be what you need:
Request.Url.AbsoluteUri
Request.RawUrl will return the application relative path(including querystring info) while Request.Url will return the complete path(including querystring info).
For more information, see "Making sense of ASP.NET paths".
Well, you could just encode it back to the url-encoded version.
Get the url from the request and urlencode only the query string part and then concatenate them

Resources