Generated URL by Html.RouteLink with special characters - asp.net-mvc

I have the following line
#Html.RouteLink(type.Description, "ListingsWithTypeSpecified", new { country = Model.CountryCode, state = Model.State, city = Model.CurrentCity.Name.ToLower(), description = type.Description.ToLower(), id = type.TypeID })
which produces
http://some.com:9609/ca/on/london/physiotherapy%20%20%26%20acupuncture/2
and
http://some.com:9609/ca/on/london/health%20department/19
first one has spaces and a &, second one just has a space
for the first one, I would like to still show
http://localhost:9609/ca/on/london/physiotherapy and acupuncture/2
for this one I understand replacing the & with "and" will work, however I still do not want %20 as spaces instead I would like to have a clean url.
Which method should I be using to properly have friendly url shown by what Html.RouteLink creates?

Replace the space with a -
e.g.
type.Description.ToLower().Replace(" ", "-")

Related

Parse a string with specific condition

How can I trim my string which is in this form:
https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg
to this?
media/93939393hhs8.jpeg
I want to remove all the characters before the second to last slash /.
I can use stringByTrimmingCharactersInSet but I don't know how to specify the condition that I want:
let trimmedString = myString.stringByTrimmingCharactersInSet(
NSCharacterSet.whitespaceAndNewlineCharacterSet() // what here in my case ??
)
The above is for removing the white spaces, but that is not the case here.
Since the string is an URL get the path components, remove anything but the last 2 items and join the items with the slash separator.
if let url = NSURL(string:"https://xxx.kflslfsk.com/kjjfkskfjksf/v1/files/media/93939393hhs8.jpeg"), pathComponents = url.pathComponents {
let trimmedString = pathComponents.suffix(2).joinWithSeparator("/")
print(trimmedString)
}
You're not trimming, you're parsing.
There's no single call that will do what you want. I suggest writing a block of code that uses componentsSeparatedByString("\n") to break it into lines (one URL per line), then parse each line separately.
You could use componentsSeparatedByString("/") on each line to break it into the fragments between your slashes, and then assemble the last 2 fragments together.
(I'm deliberately not writing out the code for you. You should do that for yourself. I'm just pointing you in the right direction.)
You might also be able to use NSURLComponents to treat each line as a URL, but I'm not sure how you'd get the last part of URL before the filename (e.g. "media " or "lego") with that method.

URL Mapping - Replacing characters in a parameter pulled from a database

I am currently trying to figure out, how to modify the parameter being integrated into the URL Mapping I am using.
static mappings =
{
"/$controller/$action?/$id?/(.$format)?"
{
constraints {
// apply constraints here
}
}
name test1: "/.../$title/..."{
controller = "study"
action = "st_show"
}
name test2: "/.../$title/..."{
controller = "search"
action = "se_show"
}
The parameter $title is pretty much a dataset, which is pulled from a database and which will get transmitted in the following format [ this is a title ]. So there are square brackets in front and behind the string and words are seperated through blanks.
If I am creating a link through g:link now with the params nested in, it gets put into the url as it is pulled from the database. What I am attempting is to create SEO-URLs, which will present a certain title of a publication devided by hyphens instead of url-encoded "%20".
Until now, I was able to generate dynamic urls looking like this:
http://localhost:8080/projectname/show/%5BAllgemeine%20Bevölkerungs[...]/782/...PARAMS...
Furthermore I already implemented it through JQuery, though it should be static and users should be able to copy the link to open up the page themselves - that wouldn't be possible when changing the url client-side while loading up the page.
Is there a way to define a function with something like replaceAll.(' ', '-'), which can be invoked onto the parameter in the mapping to replace blanks with hyphens and f.e. square brackets with an empty character?
That's pretty much, what I wasn't able to come by through the documentation.
Thank you already in advance for your help!
I managed to solve my problem by creating a service with a function containing a regex and executing this function onto the parameter title in my g:link, which I firstly converted to a string, which gets passed to the function.
<g:link controller="study" action="st_show" params="[data: data, ... title: ConversionService.convert(fieldValue(bean: path).toString(), ... data: data)]"></g:link>
And the function in the ConversionService
public static String convert(String title){
title = title.replaceAll("\\s", "-").replaceAll("[^0-9a-zA-Z\\-]", "");
return title;
}

How to deal with dynamic urls with special characters like single quote?

I am generating dynamic an "a href" html tag on my asp page. Also the url is dynamic. Sometimes there are special characters inside the url and the hyperlink is not working. For example when there is an single quote:
http://myCompany.com/'s-hertog.aspx
How can I fix this that the dynamic url always will work?
I already try this, but is not working:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href='{0}' >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);
I found the solution by my self. I changed the single quotes to double quotes in the string.format:
string hyperLinkHtml = string.Format("<span class=\"bw-NewsQueryWebpart-BodyItemTitle\"><a href=\"{0}\" >{1}</a>", HttpUtility.UrlEncode(newsItem.Url), newsItem.Title);

Remove space from query params in url dynamically

I want to remove the space in query params in the request url in ruby
Here is my sample request url:-
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'
I want to my output as below:-
URL = 'www.test.com/a?q1=john&q2=US&q3=92832832&q4=test&q5=foo'
I suggest trimming the white space. This can be achieved as stated by Joel:
If you want to remove only leading and trailing whitespace (like PHP's trim) you can use .strip, but if you want to remove all whitespace, you can use .gsub(/\s+/, "") instead.
(Ruby function to remove all white spaces?)
To remove whitespace you can use the following on the string
URL = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub(/\s+/, "")
url = 'www.test.com/a?q1=john&q2=US&q3= 92832832&q4=test&q5= foo'.gsub!(/\s+/, "")

Cleaning a URL with PHP

I've been looking though google and stackflow for an answer for this and testing a few finds but I still can't get this working.
All of these end my link at a space. For example www.website.com/movies/movie
Where I'm trying to get it to read www.website.com/movies/movie with spaces here.mp4
$namehref = "movie/" . $dirArray[$index]. " download";
$DoStream = "Watch";
$DoDownload = "Download";
However this code does not remove the spaces???
$name = $dirArray[$index];
$movienameonly = substr($name, 0, -4);
example www.website.com/movies/movie with spaces here
So my questions are - Why does the first section of code remove the spaces and how do I correct it. In addition to spaces I also hit errors with 's as well.
example They're here.mp4
To remove the spaces completely:
preg_replace("/\s/", "", $your_url);
To replace the spaces with %20 (best way):
preg_replace("/\s/", "%20", $your_url);
To replace spaces with + like url_encode($url) does:
preg_replace("/\s/", "+", $your_url);
To replace ' with %27:
preg_replace("/\s/", "%20", $your_url);
You get errors because spaces can't be inputted in the browser and converts the spaces to %20 and the apostrophe to %27
I found it:
$DoStream = "Watch";
Should have been
$DoStream = "<a href='$the_dir'>Watch</a>";

Resources