i use server.transfer at the begining it works perfectly. but then i add another language to my site and i try to do it but it fails with the new language
my code is below
if (Request.RawUrl.Contains("/tr/"))
{
Server.Transfer("tr/" + dt.Rows[0]["SourceURL"].ToString());
}
else if (Request.RawUrl.Contains("/en/"))
{
Server.Transfer("en/" + dt.Rows[0]["SourceURL"].ToString());
}
the "tr" transfers work superb but en fails it stays on my pagenotfound and not transfer to the destination url. i also check to write the whole url like http://mysite.com/en/test.aspx?k=13 and it also works but when server.transfer it fails
can anybody say why?
thanks
I think Response.Redirect will work for in your situation for the website " www.mysite.com/en/home"
It won't be as efficient as Server.Transfer, but should work just as well. I'm not sure why it works with "tr/", but not "en/", this might be due to some issue with relative paths. You could try to include a tilda "~" in front of your URL.
Server.Transfer("~/en/" + dt.Rows[0]["SourceURL"].ToString());
Related
I wish to load a local html page in my web form.
The code I'm using looks like
Response.Redirect("C:\Player\Results\xyz.html", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
I know there are similar questions asked but none helped me.
It would be very much helpful if someone can let me know where I'm going wrong
Thank you
#m_beta you are using the physical path for redirection. This will necessiate you to change the code whenever you move your code location. Use relative path instead.
It would be something like below. You need to correct the path according to your location.
Response.Redirect("~/admin/paths.aspx", false);
Redirecting to a http / https url that will respond with the static content is one thing, but no browser is going to load a local file. If browsers did that it would introduce a massive security risk.
You can add redirection like this also
If localhost, check your localhost value add like below
Response.Redirect("http://localhost:51043/xyz.html");
Live site
Response.Redirect("http://xyzdotcom/xyz.html");
A simple way might be :
var htmlContent = System.IO.File.ReadAllText(#"C:\Player\Results\xyz.html");
Response.Write(htmlContent);
Use IFrame and use it;s SRC property to call your html page
There are many ways to do it but I would like to mention the 2 which worked for me
In this approach the response will be redirected to the page you are passing.
Response.Redirect("~/Results/xyz.html", false);
HttpContext.Current.ApplicationInstance.CompleteRequest();
In this below mentioned approach the content of the html page which you wish to render will be read and then passed on using OutputStream.
var encoding = new System.Text.UTF8Encoding();
var htm = System.IO.File.ReadAllText(Server.MapPath("/Results/Html/") + "xyz.html", encoding);
byte[] data = encoding.GetBytes(htm);
Response.OutputStream.Write(data, 0, data.Length);
Response.OutputStream.Flush();
Thanks to everyone who has contributed here!
I have the following line of aspx link that I would like to encode:
Response.Redirect("countriesAttractions.aspx?=");
I have tried the following method:
Response.Redirect(Encoder.UrlPathEncode("countriesAttractions.aspx?="));
This is another method that I tried:
var encoded = Uri.EscapeUriString("countriesAttractions.aspx?=");
Response.Redirect(encoded);
Both redirects to the page without the URL being encoded:
http://localhost:52595/countriesAttractions?=
I tried this third method:
Response.Redirect(Server.UrlEncode("countriesAttractions.aspx?="));
This time the url itself gets encoded:
http://localhost:52595/countriesAttractions.aspx%3F%3D
However I get an error from the UI saying:
HTTP Error 404.0 Not Found
The resource you are looking for has been removed, had its name changed, or
is temporarily unavailable.
Most likely causes:
-The directory or file specified does not exist on the Web server.
-The URL contains a typographical error.
-A custom filter or module, such as URLScan, restricts access to the file.
Also, I would like to encode another kind of URL that involves parsing of session strings:
Response.Redirect("specificServices.aspx?service=" +
Session["service"].ToString().Trim() + "&price=" +
Session["price"].ToString().Trim()));
The method I tried to include the encoding method into the code above:
Response.Redirect(Server.UrlEncode("specificServices.aspx?service=" +
Session["service"].ToString().Trim() + "&price=" +
Session["price"].ToString().Trim()));
The above encoding method I used displayed the same kind of results I received with my previous Server URL encode methods. I am not sure on how I can encode url the correct way without getting errors.
As well as encoding URL with CommandArgument:
Response.Redirect("specificAttractions.aspx?attraction=" +
e.CommandArgument);
I have tried the following encoding:
Response.Redirect("specificAttractions.aspx?attraction=" +
HttpUtility.HtmlEncode(Convert.ToString(e.CommandArgument)));
But it did not work.
Is there any way that I can encode the url without receiving this kind of error?
I would like the output to be something like my second result but I want to see the page itself and not the error page.
I have tried other methods I found on stackoverflow such as self-coded methods but those did not work either.
I am using AntiXSS class library in this case for the methods I tried, so it would be great if I can get solutions using AntiXSS library.
I need to encode URL as part of my school project so it would be great if I can get solutions. Thank you.
You can use the UrlEncode or UrlPathEncode methods from the HttpUtility class to achieve what you need. See documentation at https://msdn.microsoft.com/en-us/library/system.web.httputility.urlencode(v=vs.110).aspx
It's important to understand however, that you should not need to encode the whole URL string. It's only the parameter values - which may contain arbitrary data and characters which aren't valid in a URL - that you need to encode.
To explain this concept, run the following in a simple .NET console application:
string url = "https://www.google.co.uk/search?q=";
//string url = "http://localhost:52595/specificAttractions.aspx?country=";
string parm = "Bora Bora, French Polynesia";
Console.WriteLine(url + parm);
Console.WriteLine(url + HttpUtility.UrlEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(url + HttpUtility.UrlPathEncode(parm), System.Text.Encoding.UTF8);
Console.WriteLine(HttpUtility.UrlEncode(url + parm), System.Text.Encoding.UTF8);
You'll get the following output:
https://www.google.co.uk/search?q=Bora Bora, French Polynesia
https://www.google.co.uk/search?q=Bora+Bora%2c+French+Polynesia
https://www.google.co.uk/search?q=Bora%20Bora,%20French%20Polynesia
https%3a%2f%2fwww.google.co.uk%2fsearch%3fq%3dBora+Bora%2c+French+Polynesia
By pasting these into a browser and trying to use them, you'll soon see what is a valid URL and what is not.
(N.B. when pasting into modern browsers, many of them will URL-encode automatically for you, if your parameter is not valid - so you'll find the first output works too, but if you tried to call it via some C# code for instance, it would fail.)
Working demo: https://dotnetfiddle.net/gqFsdK
You can of course alter the values you input to anything you like. They can be hard-coded strings, or the result of some other code which returns a string (e.g. fetching from the session, or a database, or a UI element, or anywhere else).
N.B. It's also useful to clarify that a valid URL is simply a string in the correct format of a URL. It is not the same as a URL which actually exists. A URL may be valid but not exist if you try to use it, or may be valid and really exist.
I'm trying to do some work with fakemailgenerator, the url goes well with httpwebrequest and gets printed by MessageBox.Show properly, here is the piece of code with the problem, btw there no errors or exeptions.
//FOR EXAMPLE mail#fakemail.com
string[] mailSplit = mail.Split(new string[] { "#" },
StringSplitOptions.None); // MAKING AN ARRAY TO SPLIT USER
AND DOMAIN
string url = #"http://www.fakemailgenerator.com/#/" +
mailSplit[1] + "/" + mailSplit[0] + "/"; //GENERATING AND SAVING THE FAKE MAIL URL.
MessageBox.Show(url); //THIS PRINTS http://www.fakemailgenerator.com/#/fakemail.com/mail
Process.Start("chrome", url); //THIS GOES TO http://www.fakemailgenerator.com/#/fakemail.com
EDIT
This have nothing to do with fakemailgenerator.com, because as mentioned above i tried that with httpwebrequest, plus in the loading state it's just http://www.fakemailgenerator.com/#/fakemail.com and not the full url.
EDIT
I tried rightnow putting the url manually and it went good and have been opened in chrome successfully, and i have observed one problem with the url when printed with MessageBox.Show (while using variables, not setting url manually), is showing url like http://www.fakemailgenerator.com/#/domain.com /userwith a whitespace between .com and /user, so i've tried replacing the white space with \0 (null) using url.Replace(' ','\0'), but this failed, so i think maybe there is a way to remove the white space?
Ran the code and it worked fine. A new Chrome window opened with the correct (full) url. It's an error page for me though, but if the site really exists when you try to reach it, perhaps there is some kind of a redirect that redirects you to a site with the shorter url.
I've figured around the problem, i don't really know where it's coming from, but all i know is that a whitespace was being added to the url in a way that makes process.Start("chrome",url); receives only the part before the whitespace; http://www.fakemailgenerator.com/#/domain.com/ , so i've just removed the whitespace with url = url.Replace(" ",string.Empty); and now the code works just fine.
I have a SharePoint application that uses a URL to access a resource.
code:
return web.GetList(web.ServerRelativeUrl + "/lists/UserSettings");
This works fine when I'm on a subsite, because ServerRelativeUrl will return "/sitename"
However, when this is a root site, ServerRelativeUrl will return "/" instead, leading to a poorly formed URL such as "//lists/UserSettings"
I know this seems like a simple problem (and it is) but is there a best practice for approaching this situation that is consistently safe?
Have you seen the System.Uri and System.UriBuilder classes?
new Uri(web.ServerRelativeUrl, "/lists/UserSettings");
Okay, hrm...try:
Path.Combine(web.ServerRelativeUrl, "/lists/UserSettings");
Use absolute url, this works for me:
web.GetList(web.Url + "/lists/UserSettings")
return web.GetList( (web.ServerRelativeUrl == "/" ? string.Empty : web.ServerRelativeUrl) + "/lists/UserSettings");
This might sound like a trivial problem but for some reason it is not.
Instead of needing to redirect
Response.Redirect("~/folder1/folder2/some.aspx")
I need the location as if it behaved like
string navigatingUrl = Response.Redirect("~/folder1/folder2/some.aspx")
Trying to replicate this I started with
string navigatingUrl = new Uri(HttpContext.Current.Request.Url,
new Uri("~/folder1/folder2/some.aspx", UriKind.Relative));
This instead generates "http://www.fullRequestUrl/~/folder1/folder2/some.aspx"
Edit: Well I've found out why I absolutely hate the URL API from Microsoft. I wish hellfire to whoever wrote this line of code
else if (uriKind == UriKind.Relative)
{
// Here we know that we can create an absolute Uri, but the user has requested
only a relative one
e = GetException(ParsingError.CannotCreateRelative);
}
What would possess someone to throw an error like that? This single if statement completely destroys the API.
I think you are looking for Control.ResolveUrl(). Typically you would probably use the method found on your Page object (if you are using WebForms).
Stealing from Get absolute url for a file in my asp.net project to include HTTP// etc for use externally? the only absolute way to do this is:
string url = Request.Url.GetLeftPart(UriPartial.Authority)
+ VirtualPathUtility.ToAbsolute(relativePath)
Response.Redirect(Page.ResolveUrl("~/folder1/forlder2/some.aspx"), false);