How do I instruct Grails to always create URL:s that ends with slash (/)? - grails

Consider the following Grails URL mapping:
class UrlMappings {
static mappings = {
"/something/${foo_id}/" {
controller = "foo"
action = "bar"
}
}
When generating URL:s using g:link ..
<g:link controller="foo" action="bar" params="[foo_id: 123]">foobar</g:link>
.. the resulting link looks like ..
foobar
Note that the ending slash in the URL mapping is removed.
However, both the URL:s /something/123 and /something/123/ still work.
Due to the requirements of the application I'm building I must make "ends with slash"-version of the URL the primary one. Ideally I'd like to make the URL not ending with slash return a 404 (to avoid canonical page issues).
What is the best and most general way to make Grails create URL:s where ending slashes are not removed as described above?
One way to solve it would be to create all URL:s manually, but I do not want to go that way.

Unfortunately grails URL mapping mechanism is not that sophisticated. So while forward URL mapping will work well (that is from URL to the controller) the reverse must be done manually to achive the described outcome.
Probably the best approach would be to create your own tag to output the desired links.

If you are looking for a way to create a groovy tag, that always add slash(/) add the end of the url, your best bet is to create a grails custom tagLib which create links, like the way g:link do.
But I don't know if that stops navigating by the link with out the slash.

Related

How can I show the name of the link without http://, https://, and everything that goes after .com and other similar domains?

In my view I'm displaying the link in a such way:
<%= #casino.play_now_link %>
So, #casino.play_now_link can be like this: https://www.spinstation.com/?page=blockedcountry&content=1 What I need, is to display only this part: www.spinstation.com. I tried gsub('http://', '').gsub('https://', ''), and it works, but how can I remove the part of url name after .com? Thanks in advance.
Don't use regexes at all for this sort of thing, use URI from the standard library:
URI.parse(#casino.play_now_link).hostname
or, for a more robust solution, use Addressable:
Addressable::URI.parse(#casino.play_now_link).hostname
Of course, this assumes that you've properly validated that your play_now_links are valid URIs. If you haven't then you can add validations that use URI or Addressable to do so and either clean up existing play_now_links that aren't valid URIs or wrap the parsing and hostname extraction in a method (which is a good idea anyway) with some error handling.
In a simple way one can use
.split('/')[2]
which is regex based and depends on the '/' in your url.
But as #mu is too short mentioned: URI is better for this.

Using optional parameters in Umbraco 7 Urls

Am new to using Umbraco. I need to create Urls with an an optional parameter on the end e.g.
mysite.com/people/john
mysite.com/people/jane
etc
however by default Umbraco appears to require a separate page for each person. Is there a built method in Umbraco that will allow me to define the last part of the Url as an optional parameter or do I have to write a custom route for it?
Thanks
You have a couple of options here.
Use IIS URL Rewriting to rewrite your URLs under the hood and rewrite /people/john to /people/?person=john say. Then you can pick up the person from the query string on the page.
Write a custom URL Finder that looks for the URLs and does some stuff under the hood, like get the people page, and then set a context item with the person name in for you to use in your views etc.
You could write a custom route for it. Custom routing in Umbraco is slightly different to in normal MVC. Here is a blog post detailing how you can do it: http://shazwazza.com/post/custom-mvc-routes-within-the-umbraco-pipeline/

Create external link in Asp.Net MVC3

I know there are a lot of utility and helper classes/methods for generating URLs and links from internal routes and controllers. But how would you tackle the below in MVC 3?
In a razor file someone has defined this:
Website
ExternalURL in this instance will hold values like www.yoursite.com, without any prefixes. Hard-coding an http:// at the start is an obvious no-no but how best to handle this?
It's not so bad to hardcode http:// in your case, but if you want to avoid it, I see few options, but maybe most correct will be to extend your model with property #Model.Details.ExternalUrlLink or something like that. In getter you can do any logic what you want over original value, e.g. concatenate http:// prefix if it's not presented

Implementing a single module controller while leaving the controller out of the URL

I'd like to have one module controller implement the different action methods, but I don't want the URLs to come out like
www.example.com/module/index/action1
www.example.com/module/index/action2
www.example.com/module/index/action3
where /index/ takes us to the "IndexController". To have the URLs like I want
www.example.com/module/action1
www.example.com/module/action2
www.example.com/module/action3
I would need to create a controller class for every action method. What is the best way to get the URLs I want with the different action methods in one nice class/file/controller? I was wondering if there was a way besides URL rewrites. If not, could you point me to a good URL rewriting tutorial for Magento?
There is no real reason to violate Magento reuter/controller/action scheme. For any deviation they created the URL rewrites functional.
You can create Url rewrite even omitting the module part. Creating URL rewrites is really simple, I don't think you need tutorial for that, just open admin panel, go to Catalog->Url Rewrites Management. Click on create new rewrite, choose Custom type, add the desired uri part to Request Path (module/action), and the actual uri to the Target Path (module/controller/action).
If you don't want to do an URL rewrite at the web server, you can also do, essentially, an URL rewrite inside Magento's code. For example, you can override the function Match within Mage_Core_Controller_Varien_Router_Standard like so:
/* after getting the controller name and action name... */
if ((strcmp($module, 'myModuleName') == 0) && (strcmp($action, 'index') == 0)) {
$action = $controller;
$controller = 'index';
}
/* before checking if this place should be secure and instantiate controller class
I think URL rewrite at the web server will be cleaner and more manageable, though.
If you are using Magento's URL rewrite, you'll probably need to specify a rule for every action, such as:
Type: custom
Request Path: myModuleName/hello
Target Path: myModuleName/index/hello
You can specify these URL rewrite rules in the admin, under Catalog > URL Rewrite Management

Allow plus sign in URL with MVC 3

I need to be able to allow the "+" sign for certain actions in a controller. I am building a tag filtering engine that allows something like this (ie. stackoverflow) : /Stuff/Tagged/tag-name-1+tag-name-2+other-tag
I know I can set allowDoubleEscaping="true" in the web.config, but it is not best practices for security reasons.
I am guessing there is a way using maybe a custom filer or some other registry in the global.asax?
StackOverflow is probably treating the + as a whitespace. Most likely they map the route /Stuff/Tagged/{*tags} and call string.split() on the tags. This actually works out great if you don't allow whitespace in your tags.
+ means whitespace in an url. You should URL encode them:
/Stuff/Tagged/tag-name-1%2Btag-name-2%2Bother-tag
You can use simple replace:
string url = Url.Action("Index", "YourController");
url = url.Replace("%2b", "+");

Resources