Web API - pass double parameters to GET method - asp.net-web-api

I have the following method:
[Route("GetEditorialRequestsByCoordinates/{lat:min(-90):max(90)}/{lng:min(-180):max(180)}")]
[AutomapperExceptionApiFilterAttribute]
public HttpResponseMessage GetEditorialRequestsByCoordinates(double lat, double lng)
{
}
It works fine when I call...
GET /v1/api/request/GetEditorialRequestsByCoordinates/48/2
But I want to pass double value like this:
GET /v1/api/request/GetEditorialRequestsByCoordinates/48.999/2.777
I got an error (404 not found). Seems, it can't find appropriate method by route.
I have tried to set route by way:
[Route("GetEditorialRequestsByCoordinates/{lat:double:min(-90):max(90)}/{lng:double:min(-180):max(180)}")]
...but it does not work either.
How can I fix it?

Simply adding a / to the end of the URL corrected it for me. Looks like the routing engine is viewing it as a 2.777 file extension, rather than an input parameter.
Additionally, it looks like you can register a custom route that automatically adds a trailing slash to the end of the URL when using the built-in helpers to generate the link.
he easiest solution is to just add the following line to your RouteCollection. Not sure how you would do it with the attribute, but in your RouteConfig, you just add this:
routes.AppendTrailingSlash = true;
For more details check here.
last of all max and min function works for integer
max: Matches an integer with a maximum value. {x:max(10)}
I think it does not work for double.

Related

Problems about placeholders of Netlify redirects

I am trying to set up some URL rewrites in Netlify.
I want to set up a rewrite to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
I read the document - https://www.netlify.com/docs/redirects/#placeholders:
You can use placeholders in the origin and target paths:
/news/:year/:month/:date/:slug /blog/:year/:month/:date/:slug
This would redirect a URL like /news/2004/02/12/my-story to /blog/2004/02/12/my-story
And I have my _redirects file like this:
# Redirect old permalinks to new format
/blog/:year/:month/:date/:slug.html /blog/:year/:month/:slug.html 301!
It did work to redirect
https://example.com/blog/2019/05/15/hello.html
to
https://example.com/blog/2019/05/hello.html
But there is a weird problem, it also redirects
https://example.com/blog/2019/05/not-a-date/index.html
to
https://example.com/blog/2019/05/index.html
My question is, Is it able to (if yes, how can I) make the :date placeholder matching day only? not-a-date is not a date obviously, shouldn't :date match 01 to 31 only?
To my knowledge the placeholders aren't typed (allowing only numbers). So anything will match and it's not possible to define only days as a pattern to match.
https://www.healingislands.com/* http://www.healingisland.com:splat 301!
putting { /* } this at the end of the first link and putting { :splat 301! } this at the end just worked for me

url::to(xxx/yyy) returns different results depending on context

I'm using the URL::to call to embed a link in an outgoing mail message. What I get when I do this is something like: "baseroot/public/index.php/xxx/yyy".
And yet when I do the same call, for example, within a route call, I get "baseroute/xxx/yyy".
Any idea?
The source of URL::to resides at
http://laravel.com/api/source-class-Illuminate.Routing.UrlGenerator.html#76-98
(linked to from http://laravel.com/api/class-Illuminate.Routing.UrlGenerator.html).
I suggest you add debug printing to your copy and see what values $this->getScheme() and $this->getRootPath() yield. These must be the source of the discrepancy, apparently caused by different this objects.
I had a very similar problem with URL::to('user/123') returning an incorrect value when visiting the homepage vs. another page. After some investigation, in my case it was a matter of case-sensitivity (!) in the request's url. I hope it's somehow related to your mysterious case.
More about my case: URL:to('user/123') gave me different results whether I visited http://localhost/MyApp/public/someurl or http://localhost/Myapp/public/someurl. In the former it gave the correct result http://localhost/MyApp/public/user/123, while the latter gave the wrong result of http://localhost/user/123.
.
From here, less important notes from my investigation, for future Laravel archaeologists. I hope I'm not talking all nonsense. I am new to Laravel, using a local Laravel 4 installation + WAMP on a Windows machine.
UrlGenerator's to() method uses $root = $this->getRootUrl($scheme);. The latter uses $this->request->root();, where request is \Symfony\Component\HttpFoundation\Request.
Request::root() indeed defaults to a wrong value e.g. http://localhost when visiting someurl with the incorrect case.
The culprit is Symfony\Component\HttpFoundation\Request (in vendor\symfony\http-foundation\Symfony\Component\HttpFoundation\Request.php). Its getBaseUrl() calls prepareBaseUrl(), and there the actual logic of comparing the requestUri with the baseUrl is finally performed.
For the few archaeologists still following, in my case the $baseUrl was /MyApp/public/index.php while the $requestUri was /Myapp/public/someurl, which sadly led the code to not satisfy this conditional:
if ($baseUrl && false !== $prefix = $this->getUrlencodedPrefix($requestUri, dirname($baseUrl))) {
return rtrim($prefix, '/');
}

zend framework 2 Set TextDomain in onBootstrap

I followed the instructions of this link successfully, now my web is multilanguage without requiring put "locale" in the "traslate()" calls.
But I have to put the TextDomain each time that I call it.
$this->traslate("Hello", __NAMESPACE__) //where __NAMESPACE__ is the text domain.
I would like set TextDomain in the onBootstrap method instead of put it in each call of the the "traslate()" helper.
I have tried with setTextDomain method, but it doesn't exist.
Somebody know how do it?
The onBootStrap Code is following:
.....//Code for define $locale.
$sm = $e->getApplication()->getServiceManager();
$translator = $sm->get('translator');
$translator->setLocale($locale);
$traslator->SetTextDomain($textdomain); //This line not work!!!!!
Didn't see this right the first time. Going by DASPRIDS Presentation about ZF2 I18N the correct function to call is:
$this->plugin('translate')->setTranslatorTextDomain('module-b');
Though if i see this correctly, that's from within the view Scripts. Getting the Translator from ServiceManager however - i haven't tested this - but try the following:
$translator->getPluginManager()->get('translate')->setTranslatorTextDomain('foo');
Okey. We have advanced one step.
The first solution works ok (the view solution), now my web page traduce texts only using this helper parameters, being Locale and TextDomain defined by the config:
$this->translate('HELLO');
But the second solution not works. I don't understand because the same plugin is accepted in the view and not in the onBootstrap when the name is the same.
I rewrite my onBootstrap code bellow:
$translator = $e->getApplication()->getServiceManager()->get('translator');
$pm = $translator->getPluginManager(); //until here works ok.
$pm->get('translate'); //this throws an error message how if 'translate' not found.

Trouble creating custom routes in Ruby on Rails 3.1

I can't seem to set up a custom URL. All the RESTful routes work fine, but I can't figure out how to simply add /:unique_url to the existing routes, which I create in the model (a simple 4 character random string) and will serve as the "permalink" of sorts.
Routes.rb
resources :treks
match ':unique_url' => 'treks#mobile'
Controller
.
.
def mobile
#trek = trek.find(params[:id])
end
Is this because I'm trying to define a custom action on an existing resource? Can I not create custom methods on the same controller as one with a resource?
By the way, when I change routes.rb to match 'treks/:id/:unique_url' => treks#mobile it works fine, but I just want the url to simply be /:unique_url
Update It seems like find_by_[parameter] is the way to go...
I've been playing in console and I can't seem to get any methods to come forward...I can run Trek.last.fullname for example, but cannot run #trek = Trek.last...and then call...#trek.lastname for example. Any clues why? I think this is my issue.
So is there a field on Trek which stores its unique url? If so you should be doing something like this:
#trek = Trek.find_by_url(params[:unique_url])
trek.find_by_unique_url( params[:unique_url] ) # should do the trick
#pruett no, the find_by_XXX methods are generated on-the-fly via Ruby's method_missing call! So instead of XXX you can use any of the attributes which you defined in a model.
You can even go as far as listing multiple attributes, such as:
find_by_name_and_unique_url( the_name, the_unigue_url)
Check these pages:
http://guides.rubyonrails.org/active_record_querying.html
http://m.onkey.org/active-record-query-interface
if you get a undefined method ... for nil:NilClass , it means that the object you are trying to call that method on does not exist, e.g. is nil.
You probably just missed to put an if-statement before that line to make sure the object is non-nil
Hmm. I usually would do something like this:
map.connect "/:unique_url", :controller => "treks", :action => "mobile"
Then in that controller the ID isn't going to be applicable.. you'd need to change it to something like this:
def mobile
#trek = trek.find_by_unique_url(params[:unique_url])
end
(that's if unique_url is the column to search under)

How to use params with slashes with Sinatra?

Playing with sinatra, I'm stuck on a little problem : when I use params with slashes, it confuses the router engine. So is there a nice way to handle this kind of param without having to encode it ?
The code looks like
get 'add/:url' do
#....
end
And I intend to get something like /add/http://sctackoverflow.com/ working
Did you try to use splat parameters?
Something like:
get '/add/*' do
protocol = params[:splat].first
address = params[:splat][1..-1].join('/')
url = protocol + "//" + address
end
thank you, I haven't heard about splat parameters and it works perfectly for this case. Indeed, I've looked into the documentation and I found even shorter using capture parameters and regular expressions :
get %r{/add/(.+)} do
url = params[:captures]
end
or use:
url = request.fullpath[5..-1]

Resources