Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a trailing slash at the end of each url?

I have a little problem here. I need to add a trailing slash at the end of each url in the site I'm working on. I defined all the links inside the site to have a trailing slash like so:

<a href="/register/">Register</a> 

While this works fine there's still one tiny issue: it's with the generated urls that come from calling RedirectToAction(). For example:

return RedirectToAction("Register", "Users"); 

Will cause the url to change to /register with no trailing slash. I modified my routing system as so:

  routes.MapRoute("register",                         "register/",                         new {controller = "Users", action = "Register"}             ); 

Still the required trailing slash doesn't get added automatically!
I looked up this question and this question but these are totally different as I'm not using any url rewriting libraries, i'm just using asp.net mvc routing system.
So what do you say guys?

like image 274
Galilyou Avatar asked Sep 06 '09 09:09

Galilyou


People also ask

How do you add a trailing slash to a URL?

A trailing slash is a forward slash (“/”) placed at the end of a URL such as domain.com/ or domain.com/page/. The trailing slash is generally used to distinguish a directory which has the trailing slash from a file that does not have the trailing slash.

Should I add trailing slash to URL?

If your site has a directory structure, it's more conventional to use a trailing slash with your directory URLs (for example, example.com/directory/ rather than example.com/directory ), but you can choose whichever you like. Be consistent with the preferred version. Use it in your internal links.

Why is there a slash at the end of a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.

How do I add a trailing slash to my WordPress URL?

To do so, go to the “Settings -> Permalinks” section and, depending on your situation, either add or delete the last slash. The “Custom Structure” field ends with a slash, so all other WordPress URLs will have the trailing slash. Likewise, if it is not present there, the slash will be missing in your website's URLs.


1 Answers

The RouteCollection Class now has a AppendTrailingSlash boolean which you can set to true for this behavior.

like image 118
Schmidty Avatar answered Oct 15 '22 21:10

Schmidty