slash character ignored when send query string for asp.net - asp.net

ResponseHelper.Redirect("popup.aspx?file= "+ LogicLayer.ManualPath + _ddlPLCs.SelectedValue.ToString() + "\\" + _PLCRow[0][0].ToString() ,"_page", "menubar=0,width=100,height=100");
in the second page :
if (Request.QueryString["file"] != null)
{
LogicLayer.viewManual(Request.QueryString["file"].ToString());
}
i found that slash (\) characters is removed from the file path
are there any idea ???

The backslash (\) is not acceptable in a URL. You have to encode the characters to a %HEX value. In ASP.Net there is a method to encode a URL string, and one to decode the string.
In the View:
ResponseHelper.Redirect("popup.aspx?file= "+ System.Web.HttpUtility.UrlEncode(LogicLayer.ManualPath + _ddlPLCs.SelectedValue.ToString() + "\\" + _PLCRow[0][0].ToString()) ,"_page", "menubar=0,width=100,height=100");
In the Code Behind:
if (Request.QueryString["file"] != null)
{
LogicLayer.viewManual(HttpServerUtility.UrlDecode(Request.QueryString["file"].ToString()));
}
Here's a similar question.

Related

Apex Callout HTTP Version not supported

I am working on deleting the file from the Amazon from sfdc.
I have written the code for it when am sending the request to Amazon It through error:-
System.HttpResponse[Status=HTTP Version not supported, StatusCode=505]
kindly help me to solve this problem
How can I overcome this error to pass correct request?
Code:
Datetime expire = system.now().addDays(1);
String dateString = expire.formatGmt('yyyy-MM-dd')+'T'+ expire.formatGmt('HH:mm:ss')+'.'+expire.formatGMT('SSS')+'Z';
String stringToSign = 'DELETE\n' +
'\n' +
'\n' +
dateString + '\n' +
('https://s3.amazonaws.com/'+awsKeySet[0].Name__c+'/'+fname).replaceAll(' ', '');
stringToSign = stringToSign.replaceAll(' ', '%20');
System.debug('FINDME::stringToSign - ' + stringToSign);
Blob mac = Crypto.generateMac('HMacSHA1',Blob.valueOf(stringToSign), Blob.valueOf(+awsKeySet[0].AWS_Secret_Key__c));
stringToSign = EncodingUtil.base64Encode(mac);
//String encoded = EncodingUtil.urlEncode(stringToSign, 'UTF-8');
HttpRequest con = new HttpRequest();
con.setHeader('Authorization',+awsKeySet[0].AWS_AccessKey_Id__c+':' + stringToSign);
con.setEndPoint('https://s3.amazonaws.com/'+awsKeySet[0].Name__c+'/'+'Temporary/'+fname);
con.setHeader('Host',+awsKeySet[0].Name__c+'.s3.amazonaws.com');
//con.setHeader('Date', dateString);
con.setMethod('DELETE');
Http http = new Http();
HTTPResponse res = http.send(con);
System.debug('RES.GETBODY: ' + res.getBody() + ' RES.GETSTATUS: ' + res.getStatus() + ' CON.GETENDPT: ' + con.getEndPoint());
This kind of problem typically arises from an invalid URL in the HTTP request as can be seen in this post in Salesforce success community and this post in Salesforce StackExchange. This line here is where I would start:
con.setEndPoint('https://s3.amazonaws.com/'+awsKeySet[0].Name__c+'/'+'Temporary/'+fname);
There are references to an sobject field awsKeySet[0].Name__c and a variable fname that you resolve into your URL string.
Are you certain there are no spaces or unescaped characters in the underlying value of either of those?

X++ Append Text to String

I currently have a method that appends two strings together based on the tag in an XML file that is being loaded. I would also like to add a unique key between these two strings for parsing reasons later. Below are examples of the way it is working now and what I would like it to do.
-CURRENT: strValue~&elem.text()~&
-GOAL: strValue~&elem.text()
// If the tag is "Tag" or "Building append its text to strValue (part of item name)
elem = elemTag.selectSingleNode("ofda:Type",nsmgr);
if(elem && (elem.text() == "Tag" || elem.text() == "Building"))
{
elem = elemTag.selectSingleNode("ofda:Value",nsmgr);
if(elem)
{
strValue += elem.text() + "~&";
}
}
strValue += strValue ? "~&" + elem.text() : elem.text(); ?

Get a root domain on asp.net

how can I get only the domain, for example:
The url: http://localhost:11093/SiteA/Admin/Default.aspx
Then, I want to get only the: http://localhost:11093/SiteA/
I am using:
Path.GetFileName(Request.Url.Host)
But only get the: localhost, and trying:
Path.GetFileName(Request.Url.PathAndQuery)
But get the whole address. Thank you very much.
Try this one:
var HostAndPath = Request.Url.AbsoluteUri.Replace(Request.Uri.AbsolutePath, "")
Try something like this
Uri uri = new Uri("http://localhost:11093/SiteA/Admin/Default.aspx");
string requested = uri.Scheme + uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
You can work directly on the request URI
Uri uri = Request.Url;
string requested = uri.Scheme + uri.SchemeDelimiter + uri.Host + ":" + uri.Port;
Try this...
Page.ResolveUrl("~").ToString()

how to replace special character from string in asp.net

my code -
txtPhoneWork.Text.Replace("-","");
txtPhoneWork.Text.Replace("_", "");
txtMobile.Text.Replace("-", "");
txtMobile.Text.Replace("_", "");
txtPhoneOther.Text.Replace("-", "");
txtPhoneOther.Text.Replace("_", "");
location.ContactWork = txtPhoneWork.Text.Trim();
location.ContactMobile = txtMobile.Text.Trim();
location.ContactOther = txtPhoneOther.Text.Trim();
but it is not replacing and is there any method so that both - and _ can be replaced in single function.
.Replace() returns the string with the replacement performed (it doesn't change the original string, they're immutable), so you need a format like this:
txtPhoneWork.Text = txtPhoneWork.Text.Replace("-","");
get the replaced string in some variable
you can try this to replace multiple characters in single function
string value= System.Text.RegularExpressions.Regex.replace(value, #"[-_]", "");

Formatting JSON in ASP.NET HttpResponse

I'm sending back a bunch of image tags via JSON in my .ashx response.
I am not sure how to format this so that the string comes back with real tags. I tried to HtmlEncode and that sort of fixed it but then I ended up with this stupid \u003c crap:
["\u003cimg src=\"http://www.sss.com/image/65.jpg\" alt=\"\"\u003e\u003c/li\u003e","\u003cimg src=\"http://www.xxx.com/image/61.jpg\" alt=\"\"\u003e\u003c/li\u003e"]
What the heck is \u003c ?
here's my code that created the JSON for response to my .ashx:
private void GetProductsJSON(HttpContext context)
{
context.Response.ContentType = "text/plain";
int i = 1;
...do some more stuff
foreach(Product p in products)
{
string imageTag = string.Format(#"<img src=""{0}"" alt=""""></li>", WebUtil.ImageUrl(p.Image, false));
images.Add(imageTag);
i++;
}
string jsonString = images.ToJSON();
context.Response.Write(HttpUtility.HtmlEncode(jsonString));
}
the toJSON is simply using the helper method outlined here:
http://weblogs.asp.net/scottgu/archive/2007/10/01/tip-trick-building-a-tojson-extension-method-using-net-3-5.aspx
\u003c is an escaped less-than character in unicode (Unicode character 0x003C).
The AJAX response is fine. When that string is written to the DOM, it will show up as a normal "<" character.
You are returning JSON array. Once parsed using eval("("+returnValue+")") it is in readily usable condition.
EDIT: This code is from jquery.json.js file:
var escapeable = /["\\\x00-\x1f\x7f-\x9f]/g;
var meta = { // table of character substitutions
'\b': '\\b',
'\t': '\\t',
'\n': '\\n',
'\f': '\\f',
'\r': '\\r',
'"' : '\\"',
'\\': '\\\\'
};
$.quoteString = function(string)
// Places quotes around a string, inteligently.
// If the string contains no control characters, no quote characters, and no
// backslash characters, then we can safely slap some quotes around it.
// Otherwise we must also replace the offending characters with safe escape
// sequences.
{
if (escapeable.test(string))
{
return '"' + string.replace(escapeable, function (a)
{
var c = meta[a];
if (typeof c === 'string') {
return c;
}
c = a.charCodeAt();
return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
}) + '"';
}
return '"' + string + '"';
};
Hope this gives you some direction to go ahead.
all you need to do is to use javascript eval function to get a pure HTML (XML) markup on the front end.
i.e. in a ajax call to a webservice, this can be the success handler of tha call,
the service returns a complex html element:
...
success: function(msg) {$(divToBeWorkedOn).html(**eval(**msg**)**);alert(eval(msg));},
...

Resources