(ASP.NET) Can you do a String.Format on a web.config key/value? - web-config

This might be a god awful question but I'm not sure why it won't let me do this.
I have a URL I need to store in Web.config, which has a dynamic parameter pulled from the web page.
So I want to store:
<add key="TestURL"
value="https://test/subscribe?msisdn={0}&code=1&pass=2"/>
It doesn't let me do this. After the {0} it errors at the "&".
Can anyone let me know what I'm doing wrong here? Do I need to escape the character?

Try this instead,
<add key="TestURL" value="https://test/subscribe?msisdn={0}&code=1&pass=2"/>
Notice the escaped ampersands.

Config files are XML, and as such, require XML entities to be escaped. The problem isn't your {0} for use in formatting, it's the & that must be escaped as
&

Related

php preg_replace string doesn't work

I have tried to minify a json file with str_replace() but that doesn't work well as I used it.
//I want to minify a json file with php.
//Here I am trying to replace ", {" with ",{"
//$result = preg_replace('/abc/', 'def', $string); # Replace all 'abc' with 'def'
$new = preg_replace('/, {/', ',{', $new); //doesn't work.. why?
As for the specific issue, { is a special character in regular expressions and you need to escape it. See the Meta-characters section of PCRE syntax in the PHP manual. So change the first argument to '/, \{/'. Never mind, as #Hugo demonstrated, it should work, and without telling us how your approach failed, we can't help more.
More importantly, this is terribly error-prone. What about a JSON string like ['hello, {name}']. Your attempt will incorrectly "minify" the part inside the quotes and turn it into ['hello,{name}']. Not a critical bug in this case, but might be more severe in other cases. Handling string literals properly is a pain, the simplest solution to actually minify JSON strings is to do json_encode(json_decode($json)), since PHP by default does not pretty print or put unnecessary whitespace into JSON.
And finally, maybe you don't really need to do this. If you are doing this to save HTTP traffic or something, just make sure your server gzips responses, caches properly, etc.

forcing single quotes on XMLout()

Is there an easy way to force XMLout() of XML::Simple to use single quotes instead of double quotes for the attributes? I didn't find an according option (or I missed it...).
<Object Alias="12345" Inherit="1" Position="0">
should be
<Object Alias='12345' Inherit='1' Position='0'>
It's just a matter of conventions, no special reason for that. Also there won't be any conflict with inner quotes because they are all escaped.
why you want to do this? i dunno any good reason where it can be useful?! you can print your xml file with xml out and open it again doing while(<>){s/\"/\'/g;}

How to avoid malformed URI sequence error?

I'm working with perl. I have data saved on database as  “
and I want to escape those characters to avoid having malformed URI sequence error on the client side. This error seems to happen on fire fox only. The fix I found while googling is not to use decodeURI , yet I need this for other characters to be displayed correctly.
Any help? uri_escape does not seem enough on the server side.
Thanks in advance.
Detalils:
In perl I'm doing the following:
print "<div style='display:none;' id='summary_".$note_count."_note'>".uri_escape($summary)."</div>";
and on the java script side I want to read from this div and place it on another place as this:
getObj('summary_div').innerHTML= unescape(decodeURI(note_obj.innerHTML));
where the note_obj is the hidden div that saved the summary on perl.
When I remove decodeURI the problem is solved, I don't get malformed URI sequence error on java script. Yet I need to use decodeURI for other characters.
This issue seems to be reproduced on firefox and IE7.
you can try to use the CGI module, and perform
$uri = CGI::escape($uri);
maybe it depends of the context your try to escape the uri.
This worked fine for me in CGI context.
After you added details, i can suggest :
<div style='display:none;' id='summary_".$note_count."_note'>".CGI::escape($summary)."</div>";
URL escaping won't help you here -- that's for escaping URLs, not escaping text in HTML. What you really want is to encode the string when you output it. See the Encode.pm built-in library. Make sure that you get your charset statements right in the HTTP headers: "Content-Type: text/html; charset=UTF-8" or something like that.
If you're unlucky, you may also have to decode the string as it comes out of the database. That depends on the database driver and the encoding...

Plus (+) in MVC Argument causes 404 on IIS 7.0

I have an MVC route that is giving me hell on a staging server running IIS. I am running Visual Studio 2010's development server locally.
Here is a sample URL that actually works on my dev box:
Root/CPUBoards/Full+Size
Results
Server Error404 - File or directory not found.
The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.
Here is the complete behaviour I am seeing.
Localhost:
Root/CPUBoards/Full Size - Resolves
Root/CPUBoards/Full%20Size - Resolves
Root/CPUBoards/Full+Size - Resolves
Staging Server with IIS 7.0:
Root/CPUBoards/Full Size - Resolves
Root/CPUBoards/Full%20Size - Resolves
Root/CPUBoards/Full+Size - 404 Not Found Error.
Any ideas? I need to work with the encoded version for several reasons... won't waste your time with them.
HttpUtility.UrlEncode("Full Size") returns the version with the plus sing... Full+Size. This works on my dev box, but not on the staging server. I would prefer to just get it working on the server, since I already have everything else tested and working locally, but I have no idea where to start looking on the server configuration to get it to behave the same way.
Thanks!
This is an IIS security setting. There is a standard request filter that rejects URLs containing + (plus) characters.
You can disable it for your web, adding this to your web.config:
<configuration>
...
<system.webServer>
...
<security>
<requestFiltering allowDoubleEscaping="true" />
</security>
</system.webServer>
...
</configuration>
+ only has the special meaning of being a space in application/x-www-form-urlencoded data such as the query string part of a URL.
In other parts of the URL like path components, + literally means a plus sign. So resolving Full+Size to the unencoded name Full Size should not work anywhere.
The only correct form of a space in a path component is %20. (It still works when you type an actual space because the browser spots the error and corrects it for you.) %20 also works in form-URL-encoded data as well, so it's generally safest to always use that.
Sadly HttpUtility.UrlEncode is misleadingly-named. It produces + in its output instead of %20, so it's really a form-URL-encoder and not a standard URL-encoder. Unfortunately I don't know of an ASP.NET function to “really URL-encode” strings for use in a path, so all I can recommend is doing a string replace of + to %20 after encoding.
Alternatively, avoid using spaces in path parts, eg. by replacing them with -. It's common to ‘slug’ titles being inserted to URLs, reducing them to simple alphanumerics and ‘safe’ punctuation, to avoid filling the URL with ugly %nn sequences.
System.Web.HttpUtility.UrlPathEncode(string str) encodes a + to a %20
Totally agree with #bobince, the problem is in the wrong encoding to %2b instead of %20
Sadly HttpUtility.UrlEncode is misleadingly-named. It produces + in its output instead of %20, so it's really a form-URL-encoder and not a standard URL-encoder. Unfortunately I don't know of an ASP.NET function to “really URL-encode” strings for use in a path, so all I can recommend is doing a string replace of + to %20 after encoding.
this is the important part, which is to replace the + sign with %20

cruisecontrol config.xml special characters svnbootstrapper

I have a line like :
<svnbootstrapper LocalWorkingCopy="${projects.dir}/${project.name}" Password="4udr=qudafe$h$&e4Rub" Username="televic-education" />
in my config.xml. Because of special characters in the Password cruisecontrol service won't start. Is there a way to solve this?
Maybe with setting a property? Or escaping characters?
thx, Lieven Cardoen
Replace & with & in the password attribute. I don't know if that's the problem, but it's definitely a problem.