Optional route parameters with prefix (angular ui-router) - angularjs

I need to register to a state, a route where the parameters has a prefix and a slash
(route/to/parPrefix1/:paramValue1/parPrefix2/:paramValue2)
and these parameters are all optional, so if the parameter1 is not set, the resulting url should be:
route/to/parPrefix2/value2
How can I handle this case?
Then I have a second issue:
the date should be specified like this: 20/10/2015
If i solve the first point I could set a parameter per date part, however, is there a more elegant solution?
a real case url: /risultati/destinazioni/:destinations/localita/:locations/partenza/:startingDate/ritorno/:returningDate/adulti/:adults/bambini/:children/eta/:childrenAges/aeroporti/:airports/flessibile/:flexDate/

You most likely need to set up multiple routes, that point to the same view:
ex:
route/to/parPrefix1/:paramValue1
route/to/parPrefix1/:paramValue1/parPrefix2/:paramValue2

I agree with spanndemic, see http://benfoster.io/blog/ui-router-optional-parameters.
Also I think that most of that data should be passed via services or values.
Such a long url will always make things much more difficult.

Related

Passing parameters to SSPRS Report through the URL doesn't work

I'm trying to access a SSPRS report that has the option to select the year and the month by adding the parameters in the URL as &param=value but I always get the default.
This are the parameters and I know I'm sending the correct values in the URL.
This is the report panel where I can select the Year and Month, I'm trying to get the specific report that I need by passing those parameters in the URL.
What could I be doing wrong?
Thank you everyone.
There are a couple of ways these go wrong, I'm guessing your problem is URL encoding of your date parameter, but I'll give you other stuff too. Here is a working URL with 3 parameters: a date, a string, and an integer.
https://db01.MyCompany.com/ReportServer_Prod?/Reports/R440_OutstandingRecp&paramDateEnd=12%2f31%2f2015&paramPropLiab=Property&paramRepPeriod=1
The key parts of this URL:
"https://db01.MyCompany.com/ReportServer_Prod?/" - db01.MyCompany.com is our database VM, and I'm using the "Prod" (production) instance of SQL on it.
NOTE: Check your Reporting Services Configuration application and look at the "Web Service URL" to get what "ReportServer_Prod" is on your installation.
"?/Reports/" is the path to the virtual directory, note that this is different from the path a browser would normally use. Normally my path would be "ReportServer_Prod/Pages/Report.aspx?ItemPath=%2fReports%2fR440_OutstandingRecp" if I was just viewing this from the Reporting Services interface.
Parameters are separated by "&" and it's "ParamName" "=" "ParamValue" so "&paramPropLiab=Property&paramRepPeriod=1" are the string and integer parameters respectively.
Lastly, parameter values are URL encoded if necessary. Mostly it doesn't show up, but for dates and some strings, it becomes necessary. We can't send something like "12/31/2015" because it looks like part of the path, we need a URL encoded string like "12%2f31%2f2015"
Hopefully one (or more) of these were what you needed, reply in the comments if it's still not working or if you need more explanation of why the parts are what they are.
EDIT: One more thing, if a parameter has a "Display" and a "Value" (i.e. in a drop down list) you must pass the value, not the display.
EDIT: I can't make the comment stop hiding my URL, so I'll put it here
WHAT WAS TRIED
https://slo2000/Reports_TECOVA?/Reports/TEXO%20CVA%20Reports%2fTEXO_London_B_CVA_Report&ReportMonth=January&ReportYear=2020
https://slo2000/Reports_TECOVA?/Reports/TEXO%20CVA%20Reports/TEXO_London_B_CVA_Report&ReportMonth=January&ReportYear=2020
https://slo2000/ReportServer_TECOVA?/Reports/TEXO+CVA+Reports/TEXO_London_B_CVA_Report&ReportMonth=January&ReportYear=2020
WHAT WORKS (From #Nacho in comments, brought here for visibility)
http://slo2000/ReportServer_TECOVA/Pages/ReportViewer.aspx?%2TEXO+CVA+Reports%2fTEXO_London_B_CVA_Report&rs:Command=Render&ReportMonth=January&ReportYear=2020

React-router, optional url parameter with namspace

I am trying to define a react-router with an optional url parameter prefixed with a namespace. This is an example of such a path:
path="authors/:authorId/posts/:postId?"
// application.com/authors/8/posts/4
I want the postId variable part to be optional, but this should include the whole /posts/:postId part to be optional. Is this possible?
Not sure why this is a thing that some developers want (haven't seen a good use case scenario where you would need to structure your URL as such), but the simplest solution is to instead just use a query.
http://www.example.com/authors/author?authorId=8&postId=4
Nonetheless, while the answer provided below is specific to the question, the set up for your desired URL structure is the same (see second approach in the answer): React Router v4 Nested match params not accessible at root level

Extra character while using multiple optional parameter in angular route

I am using angularjs router, as the application development is almost done, I cant use UI-router. Recently I implemented two optional parameter in route by following this answer.
Here is what I did.
app.when('/someUrl/:param1?/:param2?',{
templateUrl:'templateurl',
controller:'controllerName'
});
But when I use $location.path('/someUrl/1234/5678');, the url is adding equivalent hex code of '?' in URL either parameter is available or not.
I am not sure why this parameter is coming even if I am sending parameter.
the url is looking like
localhost/someurl/1234%3F/5638%3F
How can avoid this %3F and keep optional routing functionality without using duplicate route definitions.
**Sorry for typo mistake, I already defined routes with :, that is not problem with :.
%3F is ?, Since you have not provided the : its treated as part of URL thus they are encoded.
You need to use : to define parameter.
app.when('/someUrl/:param1?/:param2?',{
templateUrl:'templateurl',
controller:'controllerName'
});

angular.js get slash-separated path from url

I need to take as a parameter in my site's urls a file path (that will contain some number of '/' characters). How can I parse such a parameter from a url? Something like http://localhost/path/to/file would be preferable, but if that doesn't work, http://localhost/?path=/path/to/file or something could work as well.
to use location in your controller. first turn html5mode to true like this
Then you can use
location.search and it will return string without any error.
for more use of location go here https://docs.angularjs.org/api/ng/service/$location
Take a look at URI.js, it looks like what you need.

Encoding url's the Play! Framework

Is there a way to make the Play! Framework ignore slashes and ? in parts of the URL?
Typically, if I have the following:
www.123.com/api/link/http:www.bla.com/?contenId=123&User=test
It won't work. In that case, what I would want to do is simply have the link in the last part of the URL in a String variable to save it. I suppose I can force the client to replace the / and ? by something else, but I would rather keep it simple.
My first thought was that maybe there is a way to configure the routing such that we have something like:
/api/link/{data}
where data would hold whatever remains of the URL. Can't find out how to do that though.
You can't have : / ? except your main URL. You should encode your parameter to append it to main URL. See URLEncoder for Java.
This is not a valid URL:
http://www.123.com/api/link/http://www.bla.com/?contenId=123&User=test
It must be:
http://www.123.com/api/link/http%3a%2f%2fwww.bla.com%2f%3fcontenId%3d123%26User%3dtest
Then you can pass it to {data} parameter and decode it in your handler method.

Resources