How to map an URL having querystring parameters in urlmapping.groovy? - grails

I am new to Grails/Groovy. Please bear with me if this is a silly question.
I am trying to map an URL having querystring parameters in urlmapping.groovy, something like,
"/rest/employees?empno=123&dept=delivery"(controller:"employees", action="emp")
The values 123 and delivery are dynamic values. In other words, this could be anything that the user can use.
Apparently this syntax is not correct. What is the right way to achieve this?

Just leave /rest/employees"(controller:"employees", action="emp") and you'll get your query parameters as params.empno and params.dept in your action

#splix answer is correct. If you want to use a restfull url, you could do something like
"/rest/employees/$empno/$dept"
instead. Otherwise just leave out the part after "?" as said. In your controller you will still get params.empno and params.dept
Read more here

Related

URL Encode string for Href ASP.NET MVC / Razor

I'm trying to build an Href using Razor
The string is going to end up looking like this:
https://www.notmysite/controller/action?order_ID=xxxxxxx&hashComparator=iFxp3%2BszAMaEB%2FnHCtRr9Ulhv0DumtyDumCik4gKypJqi0BdOGXXsr9QDkfefAsLaR1Xy%2BrX9VcuzP1pF2k6dL%2F92UxphzTKqNAZP2SSZGWtvyO5az%2F9JvDY%2Bsq5TBQo7%2FYAAMIU4QxiSX1SBKk8SUNECW3ZmKM%3D
In my model I have the order id and the hash string
As the route is not a part of my site I don't believe I can use the default methods like #Url.Action
and therefore can't use protocol: Request.Url.Scheme
like I've used elsewhere.
So at present I'm trying to figure out how to create this using string functions
I've tried
Url.Encode
Url.EscapeDataString
Html.Encode
but am getting no where fast:
Click Here to be transferred
The output text always has plusses and equals in them and doesn't work.
Which combination do I need?!
I've figured out a way of doing it:
#{
var url = string.Format(
"https://www.notmysite.co.uk/controller/action?order_ID={0}&hashComparator={1}",
#Uri.EscapeDataString(Model.bookingNumber.ToString()),
#Uri.EscapeDataString(Model.hashCode));
}
<p>Click Here to be transferred</p>
Edit 2015 - As mentioned by Jerads post - The solution is to only encode the query string elements and not the whole URL - which is what the above does.
This was the first link that came up for this issue for me. The answers didn't work for me though because I am using core, I think. So wanted to add this in.
System.Net.WebUtility.UrlEncode(MyVariableName)
If Url.Encode doesn't work try the above. Also as stated before don't use this on the entire URL string, just use it for the individual querystring variables. Otherwise there is a good chance your URL wont work.
The problem is that you're trying to encode the whole URL. The only pieces you want to encode are the querystring values, and you can just use Url.Encode() for this.
You don't want to encode the address, the querystring params, or the ? and & delimiters, otherwise you'll end up with an address the browser can't parse.
Ultimately, it would look something like this:
Click Here to be transferred
The easier method is to use #Html.Raw(Model.SomethingUrl)

pass variable but not show in the url?

I want to pass a variable in my URL but not show it in the URL for SEO purposes
e.g www.mywebsite.com/Search?city=NYC should looks like www.mywebsite.com/Search and still be able to retrieve the value of "city" on the page.
Thanks
Simply use a POST request and you're done. However, is a common pratice to use GET request for search forms because the user can bookmark the url. Using POST, this isn't possible.
I just used Rewrite rules to accomplish this.

how to allow the bad url in ASP.NET MVC 3

i want to pass a parameter in url blank but localhost server tell that it is bad url. are i can make them work in MVC 3
the url is
http://localhost:6251/time/saturdau/first/second//nextparameter
you can see that third parameter is blank here. are this request can work whenever 4th parameter pass without passing 3rd paramter.
what i do to make this work.
rather put a work around on this say you make a convention if the parameter passed is null you may try passing something like my_conventional_null_indicator
so instead of making it look like this
http://localhost:6251/time/saturdau/value1/34//70
do this
http://localhost:6251/time/saturdau/value1/34/my_conventional_null_indicator/70
but the best way would still be the conventional way
http://localhost:6251/time/saturdau?param1=value1&param2=34&param3=&param4=70
or the much better way is to maximize the capability of the RouteValueDictionary.
This is not allowed. You can have optional parameters, but they must be the last segment in the URL. You can't have an optional parameter in the middle.

Extend Url Route to apply Url Encoding for each parameter

I am facing a problem that one of my fields need to be shown in the url contains special character (/, \, :).
The stupid way to handle this generate action links by using UrlEncode(). Then UrlDecode is used before consuming in controller. But I think it really stupid because too many places need to be adapted.
So, my problem is there any way to extend the url route or just write my own one to achieve it?
Thanks,
Mike
You can extend the System.Web.Routing.Route object to create a custom route and override the GetRouteData and GetVirtualPath methods. These are called to resolve a route's values and create a URL from given route values, respectively. However, I don't think URLs can contain URL encoded values for / (%2f) within the path portion of a URL though it is ok in a query string.

Grails g:paginate tag and custom URL

I am trying to use g:paginate in a shared template where depending on the controller, url changes e.g.
For my homepage url should be : mydomain[DOT]com/news/recent/(1..n)
For search Page: www[DOT]mydomain[DOT]com/search/query/"ipad apps"/filter/this month
and my g:paginate looks like this:
g:paginate controller=${customeController} action=${customAction} total:${total}
For the first case, I was able to provide controller as 'news' and action as 'recent' and mapped url /news/recent/$offset to my controller.
But for the search page, I am not able to achieve what I want to do. I have a URL mapping defined as /search/$filter**(controller:"search",action:"fetch")
$filter can be /query/"ipad apps"/filter/thismonth/filter/something/filter/somethingelse.
I want to be able to show the url as above rather than
?query="ipad apps"&filter=thismonth&filter=something&filter=somethingelse.
I believe I can pass all the parameters in params attribute of g:paginate but that will not give me pretty URL.
What would be the best way to achieve this? Please feel free to ask questions If i missed anything.Thanks in advance.
Ok. figured this out. Hope this helps someone.
Set ${customAction} ='' and this query string 'filter/thismonth/filter/something/filter/somethingelse' was passed with 'params' alltribute.
Thanks

Resources