I am trying to send a string with some special characters to my Asp.Net Web Api controller. However, Asp.Net can't seem to resolve the url encoded string. Sending something like "A%2F223%2F4" is the encoding for "A/223/4" and the same also doesn't work for backward slashes. It does work for other special characters though. Is there any way to get this working? Or is it possible to turn the automatic decoding off, so that I can do it manually?
This is my functions inside my controller:
[HttpGet, Route("getByArtNr/{articleNr}")]
public IHttpActionResult GetByArtNr(string articleNr)
{
Article article = dbContext.Article.Where(x => x.ArtNr == articleNr).FirstOrDefault();
if(article == null)
return NotFound();
return Ok(article);
}
Example request:
http://localhost:54282/api/v1/article/getByArtNr/A%2F223%2F4/
In order to send a string which has / (slashes) in it, we can use the wildcard parameters in the route. Something like this:
Route("getByArtNr/{*articleNr}")
This will allow for a chunk of a URL with multiple / characters to be read as a single parameter.
Hope it helps.
Related
I am working on a URL filtering project . I have a database given to me which contain URLs need to be blocked (eg: a.b.com/d/e).
I get uri and domain from http request. I compare what I get with my database and redirect users without any problem. So far so good.
Problems starts with urls that contains query string and other magics with URL. As an example if user enters a.b.com/d/e?junk. What I get won't match with my database, and users will bypass my filter and they will still be able to go a.b.com/d/e.
I tried some useless actions like slicing everything after special chars like "?,#". But having problems with url like : youtube.com/watch?v=12vh55_1ul8, which becames like youtube.com/watch and blocks all youtube. That solution causes me more problems.
Now I am very confused how to handle this problem. Is there any guide or any library which I can use in C++ ?
Try this code:
string str (get_requsted_uri());
string str2 ("http://getaroundfilters.com/article/889/proxy");
if (str.find(str2) != string::npos) {
block();
} else {
get_and_return_webpage(str);
}
I have this:
public RedirectResult LinkRedirect(string url)
{
return Redirect(url);
}
And all it does, is redirecting me to http://mysite.com/www.externalsite.com. What am I missing here?
You need to pass a url with http://prefixed to it. Then it will work
Ex: http://www.google.com
So if you want to allow your action method to accept all kind of links( with and without http prefix), you need to write little bit of code to check whether the passed the url parameter value has http prefix and if not, append it.
We have an Id that could look something like this:
WIUHyUT/Evg=/
That we would like to use in the path or an url:
http://localhost/freelancers/WIUHyUT/Evg=/Brigitte
This obviously does not work, so we used HttpUtility.UrlEncode() and get
http://localhost/freelancers/WIUHyUT%2fEvg%3d/Brigitte
But this still does not work.
What would be a good approach here?
Once you get the url string back, you have to decode it.
Also, you should use any slashes after encoded params, use ampersand instead to join them.
We actually decided to encode the whole thing into HEX first:
public static string GetBytesToString(byte[] value)
{
SoapHexBinary shb = new SoapHexBinary(value);
return shb.ToString();
}
With this we then just had HEX codes in the url. Works fine.
I have a requirement where the id I use in #RequestMapping accept a value of the format 1234567812345678 and abcxyz/abcxy+z$ (basically a auto generated string with / forward slash in it).
I have tried a mapping of all the 3 below formats but nothing works:
#RequestMapping(value="abc/action/{id: .+[//]*.*}.{format}", method=RequestMethod.PUT) -- Accepts nothing.
#RequestMapping(value=" abc/action/{id:.+}.{format}", method=RequestMethod.PUT) -- Accepts everything except /
#RequestMapping(value=" abc/action/{id:.*}.{format}", method=RequestMethod.PUT) -- Accepts everything except /
However, when I try the same regex in a normal java program, it works like a charm :
package miscellenousTest;
public class RegExTest {
public static void main(String[] args) {
String s1 ="9876543298765432";
String s2 =" abcxyz/abcxy+z$";
System.out.println(s1.matches(".+[//]*.*"));
System.out.println(s2.matches(".+[//]*.*"));
}
}
I have gone through some of the previous posts but nothing gives me the exact solution I am looking for.
It will be great if someone can throw some pointers/solution at this one.
you should not use slashes when sending data in urls. You will need to urlencode it before sending it. It will then be decoded before being given back to you.
Sometimes I make web safe names when i come across this problem. So for example i need to sometimes send ids like "12345/01". When I send them over the web in urls, I say "12345_01", which we refer to as the web safe id. we then convert it by replacing _ with / when we need the real id.
I have an MVC3 Action that takes a parameter (a URL) that may have a query string in it. My action signature looks like this:
GetUrl(string url)
I expect to be able to send it urls, and it works every time unless there is a query string in the url. For example, if I navigate to:
MyController/GetUrl/www.google.com
the url parameter comes accross as "www.google.com" -Perfect. However, if I send
MyController/GetUrl/www.google.com/?id=3
the url parameter comes accross as "www.google.com/" How do I get MVC3 to give me the whole URL in that parameter? -Including the query string?
It's simple enough to just URL.Encode the passed in URL on the page but you're opening your self to some possible security problems.
I would suggest you encrypt the url then encode it then pass that as your value, the protects you from having people just passing in anything into your app.
That's because system considers id=3 as its own query string. When you construct the link in the view, you need to use #Url.Encode to convert raw url string to encoded string to be accepted as parameter of the controller.