Generating links and anchor tags to other areas within your website is a very common requirement in a project, and luckily, the EPi API makes it very easy to do. There are several ways to ge the current page Url, so here goes:
To get the current pages Url
To get the Url for the page a user is currently on, the first thing you need to do is get a reference to the current page, this can be done using the PageRouteHelper. Once you have the current page, you can call it's LinkURL property and job done.
To get the current pages from a block
If you are in a block then you can will need to get the page data directly out of the HttpContext
To get the URL of another page
To get a referee to another page, you will need a reference to it, e.g.a ContentReference or the page ID of it. In this example, I'm going to hard-code the value in but in your production code. I would recommend either adding a property on the page type you are working on, or, to create a specific Settings Page Type that would store all my important URL's for me. On most projects, these links may include the Search Page, Contact Us Page, About Page etc...
How To Get the Friendly URL page
Using the methods above when the link is rendered on the website you will probably notice very quickly that the links are not very SEO friendly. Having friendly Url's will improve your websites SEO ratings and will generally make your website easier to use for people. In order to to render your links in a 'friendly' manner you need to do the following:
Get The External Url Of A Page
For a more in-depth overview of how friendly Url's work have a read of my other article Creating a friendly Url within your Episerver 7 MVC View Has this article solved your problem or do you think you know a better way to get a link? Leave a comment below to prove it.