How do I split out parameters from a url in Iron Router? - meteor

I have a Meteor web app. and I have URL's formatted like this:
/box/bx_0/12345/bx_1/67890/ ...etc...
I want to be able to extract the pairs like this into an array in my Router file:
boxes['bx_0'] = 12345;
boxes['bx_1'] = 67890;
How can I do this?

i am not sure if there is a pack or a convention for this, anyway you could setup a generic route, grab the url, split on "/" and then parse it.
In other words, i think doing it manually should be fast enough to justify not looking for another solution...

Related

Should a WebDAV server support query strings?

Should a WebDAV server support query strings?
I have not found a clear statement about this in RFC 4918.
Background is as follows:
I have a WebDAV server where the path in the URL is mapped quasi 1:1 to the path to the resource in the file system. I.e. to get to the resource I need to know the path, something like this:
Variant 1:
http://<webdavserver>:<port>/folder1/subfolder1/anotherfolder/resource.txt
Now I have another client that doesn't know the path, but only two Ids (RepositoryId and DocumentId), but these also uniquely identify the resource. By searching for the two ids, the WebDAV server can also find the resource and return it.
Until now, this was solved in such a way that instead of the path in the URL, the two IDs were specified as a query string, i.e. something like this:
Variant 2:
http://<webdavserver>:<port>/?repoId=123&docId=456
Somehow this feels wrong ...
Well, actually the identifcation via the two ids is just an alternative representation to the path, isn't it? So something like this should work too:
Variant 3:
http://<webdavserver>:<port>/<repoId>/<docId>
http://<webdavserver>:<port>/123/456
This feels more "WebDAV-like" ...
I would only need to be able to distinguish on the server side which of the two URL representations is arriving there, path or ID.
Possibly via a header, something like
X-ResourcePath: Path | Id (Default would be Path)
What do you think?
Should I stay with variant 2, or rather switch to variant 3, or ...? (I have to reimplement it anyway, so "Do not change a running system" would not be a valid argument :-))
IMHO: it really doesn't matter. There's no prohibition to mix query params into WebDAV URI trees. You just need to make sure that the clients that you support will work with this.
(I would advise against moving identifying data into custom request header fields; this is what the URI is for).
When I implemented a WebDAV server I was primarily interested in supporting existing clients and I found that most of them did not support query strings in any way (namely Microsoft Office)
I ended up using the following format which seems to work for all clients:
protocol://server/id/title.extension

Symfony2 GenerateURL to a complex route

This seems like the dumbest question ever, but I can't seem to figure it out. I have a page the needs to redirect to a complex route and I can't seem to generate the URL. Redirecting to a simple route is easy enough:
return $this->redirect($this->generateUrl('testnumber'));
However, I want to route to: testnumber/1/question/4. How can I accomplish this incredibly simple task? The only thing I have found in the documentation and Google allows me to add parameters and not just create a complex route. For example:
generateURL('testnumber', array('testid'=>1, 'question'=>4))
makes a URL of /testnumber?testid=1&question=4, which I do not want.
Edit: Yes, I already have the route created in a YML file. I simply cannot generate the URL to link to it.
return $this->redirect($this->generateUrl(???????????),true));
This is my route:
#Route("/testnumber/{testid}/question/{question}", name="testnumber")
The Symfony documentation only shows how to generate a URL to " testnumber/1", I need to generate "testnumber/1/question/4".
For
generateURL('testnumber', array('testid'=>1, 'question'=>4))
to work as you want, your route must look like (example using annotations)
#Route("/testnumber/{testid}/question/{question}", name="testnumber")
If you don't define "testid" & "question" parameters in your route, they'll be added to the query string (appended at the end of the URL as GET paramaters)
generated_route?test_id=X&question=X
Find here more relevent examples.

ASP.NET Routing: Formatting the URL string

I have implemented a routing functionality successfully in my project (a news website):
Sub RegisterRoutes(ByVal routes As RouteCollection)
routes.MapPageRoute("ndetails", "news/{title}/{id}/", "~/newsdetail.aspx")
End Sub
and I set the URLs like this (databound to a repeater):
href="<%# Page.GetRouteUrl("ndetails", new with { .title= Server.UrlEncode(Eval("Title")), .id= Eval("NewsID")})%>"
The URL produced is like:
/this%20is%20a%20news%20item/89
As can be seen above, the URL part is difficult to read and I would like it to be like:
/this_is_a_news_item/89
I thought of going for a Replace function. But then, since the user creating the news might enter any string, I have to take into account all the other characters that might need to be replaced.
I just wanted to know from an experienced developer, whether going with a long replace function is the way to go, or is there another solution to format my URLs in this rouitng scenario.
Many thanks in advance
AFAIK there is no built in funcitonality in the framework to make url "pretty". You have to implement your own url fo rewriting the title.
In the save of your entities simply use a function that do the replaces that you need (' ' with '_' or example) and then use UrlEncode.
You can also use a Regular expression to do the replacement in one go.

ASP.NET routing: Literal sub-segment between tokens, and route values with a character from the literal sub-segment

The reason I'm asking is because IIS protects certain ASP.NET folders, like Bin, App_Data, App_Code, etc. Even if the URL does not map to an actual file system folder IIS rejects a URL with a path segment equal to one of the mentioned names.
This means I cannot have a route like this:
{controller}/{action}/{id}
... where id can be any string e.g.
Catalog/Product/Bin
So, instead of disabling this security measure I'm willing to change the route, using a suffix before the id, like these:
{controller}/{action}_{id} // e.g. Catalog/Product_Bin
{controller}/{action}/_{id} // e.g. Catalog/Product/_Bin
But these routes won't work if the id contains the new delimeter, _ in this case, e.g.
// These URL won't work (I get 404 response)
Catalog/Product_Bin_
Catalog/Product/_Bin_
Catalog/Product/__Bin
Why? I don't know, looks like a bug to me. How can I make these routes work, where id can be any string?
Ok, I have a definitive answer. Yes, this is a bug. However, at this point I regret to say we have no plans to fix it for a couple of reasons:
It's a breaking change and could be a very hard to notice one at that.
There's an easy workaround.
What you can do is change the URL to not have the underscore:
{controller}/{action}/_{id}
Then add a route constraint that requires that the ID parameter starts with an underscore character.
Then within your action method you trim off the underscore prefix from the id parameter. You could even write an action filter to do this for you if you liked. Sorry for the inconvenience.
You can use characters that are not allowed for a directory or file name like: *,?,:,",<,>,|.
With ASP.NET MVC if you look at the source they have a hard-coded value for the path separator (/) and to my knowledge cannot be changed.

URLRewriting challenge

I'm using URLRewritingNet 2.0. How do I rewrite URL's in ASP.NET?
Here is the request:
Input: www.sampleweb.com/param1/value1/param2/value2/default.aspx
Output: www.sampleweb.com/default.aspx?param1=value1&param2=value2
It must work dynamically like this param1/value1/param2/value2/ ... /paramN/valueN
That is not a good way to pass key/value pairs.
You should assume the key based on the values position. That makes life a lot easier. HttpContext.RewritePath (with its variations) is how you go about transforming the url.
So...basically you can't do this in the "rewrite" node in your web.config file using ASP.Net's URL Rewriter.
But you could do it elsewhere in your code (an HTTP Module, or Begin Request, or whatever). To transform you URL's you could do something like this:
string strRegex= #"/([^/]*)/([^/]*)";
RegexOptions myRegexOptions = RegexOptions.None;
Regex myRegex = new Regex(strRegex, myRegexOptions);
string strTargetString = #"/param1/value1/param2/value2/param3/value3/param4/value4";
string strReplace = #"$1=$2&";
If you combine that with matching the filename (here's the RE):
(.*)/([^/]*\..*)$
And then re-composing the complete URL--then you could Server.Execute or whatever (if on your own server) or otherwise proxy over to where you want this processed. Yup, it's a little ugly, but if you don't have control of the shape of the request coming at you, this is a way to transform it.

Resources