redirection to other page with encoded url - c#

I am using one Image on my html page. where i am adding one anchor to that image to give the link.
that anchor contains href as ~/LB/lct.aspx?pid=177&cat=Happily In Love
Main Thing This URL IS COMING FROM DATABASE. Manually i am not entering it..
So its a Invalid URL Because of spaces between Happily In Love
I used Httputility.urlencoding and decoding also........but the problem i am facing is that..
Url is endoded properly but while i am clicking on the image its not redirecting to proper page because encoded url is not decoded..
How to resolve this...pls help me on this....

Here is the code
string url = "~/LB/lct.aspx?pid=177&cat=Happily In Love"; //your input
string[] arr = url.Split('?');
var nameValues = HttpUtility.ParseQueryString(arr[1]);
foreach (var n in nameValues.AllKeys)
{
nameValues.Set(n, HttpUtility.UrlEncode(nameValues[n]));
}
url = arr[0] + "?" + nameValues.ToString(); //your output
Use the following code to decode Querystring values
string cat = HttpUtility.UrlDecode(Request.QueryString["cat"].ToString());

Simple answer, but if it's just the spaces that are causing problems, consider replacing them with a + using a string replace:
string newurl = url.Replace(" ","+");
Note that this is only really safe if the spaces are restricted to the contents of a querystring.

Related

How to store the url of a web page in an sql database

I have to store the link of a page in a database. The page is in my website.
As example:
I have to store the link of Result.aspx page in the database. How could I do this? I know that google.com can easily be stored and it's working with google.com, but I want to know how to do this with Result.aspx.
I will provide another example: there is an asp panel in my website and I have to store the urls of each row of menu and sub menu. These urls are also in my website like Default.aspx, Result.aspx etc.
If any question please ask.
Your question is not clear on me, but if you want to save your current page URL,
Feel free to use this.
string URL = Path.GetFileName(Request.Path);
string sqlIns = "INSERT INTO table (url) VALUES (#url)";
db.Open();
try
{
SqlCommand cmdIns = new SqlCommand(sqlIns, db.Connection);
cmdIns.Parameters.Add("#url", URL);
cmdIns.ExecuteNonQuery();
cmdIns.Dispose();
cmdIns = null;
}
catch(Exception ex)
{
throw new Exception(ex.ToString(), ex);
}
finally
{
db.Close();
}
If I understood you correctly, the problem is not with DB itself, it is with the relative url of the page. So, if your path is http://myWebSite.com/Result.aspx and http://myWebSite.com/Default.aspx then you should save string "~/Default.aspx". If your path is like http://myWebSite.com/someRoute/Result.aspx and - then you should save string "~/someRoute/Result.aspx".
To get this route you can use the following code:
string path = HttpContext.Current.Request.Url.AbsolutePath; // /someRoute/Result.aspx
it is simple...
private HtmlGenericControl LIList(string innerHtml, string rel, string url)
{
HtmlGenericControl li = new HtmlGenericControl("li");
li.Attributes.Add("rel", rel);
**li.InnerHtml = "" + innerHtml + "";**
return li;
}
this url is link which save in database.....

C# Scrape Cookie Values From WebBrowser Control?

I have no experience with webbrowser control I'm trying to do the following:
Navigate to website and to scrape the cookie values it gives back (specific with regex). I tried to search for this here & google there is no information about this, Are the webbrowser control connected to IE? This will be impossible to read the values right? Any way to capture the cookies? Like CookieContainer?
I have one code i use with httpwebrequest it can be an example to what I'm trying to do with webbrowser control:
List<string> cookieValues = new List<string>();
foreach (Cookie cookie in agent.LastResponse.Cookies)
{
cookieValues.Add(cookie.Value);
}
foreach (string i in cookieValues)
{
Match match2 = Regex.Match(i, #"bert=([\w]*)",
RegexOptions.IgnoreCase);
// Ensure match
if (match2.Success)
{
// Finally, we get Group value and display it.
key2 = match2.Groups[1].Value;
}
Thanks for the help like always!

Input URL like http://example.com changes to http:/example.com in action input

I asked a question to get URL as action input here. Now I have a new problem. The passed URL to action changes from http://example.com to http:/example.com.
I want to know why and how can I resolve the problem.
P.S: I added this code to resolve but I think there may be another problems in future! the code is:
if ((url.Contains(":/")) && !(url.Contains("://")))
{
url = url.Replace(":/", "://");
}
The browser (or server) is replacing a double slash (illegal) with a single one.
Try it,
http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input
(in Chrome) goes to:
http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input
If I were you, I would remove the http:// from your path and add it later.
http://localhost:1619/Utility/PR/example.com/
Then, url = "http://" + url;
If you might get secure urls, add that to the route /http/example.com or /https/example.com
use regex:
string src = #"http://example.com";
string result = Regex.Replace(src, #"(?<=https?:/)/", "");
if you need to revert:
string src = #"http:/example.com";
string result = Regex.Replace(src, #"(?<=https?:)/(?=[^/])", #"//");

How to get Sharepoint image in relative url format

I have an SPListItem that I return a full image url:
http://sharepointsite.com/images/bob's picture.jpg
I return this url by calling this:
splistitem.File.ServerRelativeUrl
I want to be able to turn this url into this:
http://sharepointsite.com/images/bob%27s%20picture.jpg
but if I encode the full URL it will replace the / which i dont want. I want to be able to just get the ending image file and UrlEncode that, how would I go about solving this progmmatically?
Try this:
var url = "http://sharepointsite.com/images/bob's picture.jpg";
var basePath = System.IO.Path.GetDirectoryName(url);
var fileName = System.IO.Path.GetFileName(url);
var finalPath = basePath + "\\" + Uri.EscapeDataString(fileName);
You can achieve this by simply calling splistitem.File[SPBuiltInFieldId.EncodedAbsUrl] - it won't replace the forward-slashes. And then you can pass it to System.IO.Path.GetFileName if you want only the filename.
By the way, shouldn't ServerRelativeUrl return /images/bob's picture.jpg?

C# I want the url, not the physical pathname

For this line of code;
string link = HttpContext.Current.Server.MapPath("/Contract/Details/" + this.ContractId.ToString());
I get the physical pathname on C drive.
What I want is the url, ie
http://localhost:1234/Contract/Details/1
How do I get this?
// Use the Uri constructor to form a URL relative to the current page
Uri linkUri = new Uri(HttpContext.Current.Request.Url, "/Contract/Details/" + this.ContractId.ToString());
string link = linkUri.ToString();
try this:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
There's a great article on .Net paths # http://west-wind.com/weblog/posts/132081.aspx
Take a look at the Url or PathInfo property.
Uri base = new Uri("http://localhost:1234/";);
Uri file = new Uri(host, "/Contract/Details/" + this.ContractId.ToString());
string URL = file.AbsoluteUri;

Categories

Resources