---
title: "Optimizing URL Generation in ASP.NET MVC"  
description: "URL generation is particularly important for ASP.NET MVC because it uses a routing engine to map URLs to code. If we hard code a URL than we lose the"  
author: "AVADHESH PATEL"  
published: 2013-01-30  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/436/optimizing-url-generation-in-asp-dot-net-mvc  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 2 minutes  

---

# Optimizing URL Generation in ASP.NET MVC

URL [generation](https://www.mindstick.com/blog/300788/why-is-heart-attack-increasing-in-the-younger-generation) is particularly important for [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc) because it uses a routing engine to map URLs to code. If we hard code a URL than we lose the ability to later vary our routing scheme.

ASP.NET [MVC framework](https://www.mindstick.com/interview/34199/what-is-the-difference-between-forms-authentication-in-asp-dot-net-web-forms-and-asp-dot-net-mvc-framework) provides the following methods to generate URL. These methods help to optimized ASP.NET [MVC application](https://www.mindstick.com/forum/452/do-i-need-to-install-mvc-3-4-on-web-server-to-run-mvc-application).

1. [Html.ActionLink](https://www.mindstick.com/forum/157896/what-is-the-difference-between-the-html-actionlink-and-ajax-actionlink-helpers-in-asp-dot-net-mvc)()

2. Html.RouteLink()

3. Url.Action()

4. Url.RouteUrl()

##### Html.ActionLink()

**Html.Actionlink** method is an easiest way to render an HTML link. In MVC, the Html.ActionLink() does not link to a view. It creates a link to a [controller action](https://www.mindstick.com/forum/12778/how-to-send-data-from-view-to-controller-action-in-asp-dot-net-mvc).

Syntax: Razor

```
@Html.ActionLink("Description", "Action Name")                                 
```

##### Syntax:ASPX

```
<%=Html.ActionLink("Description ", " Action Name ")%>
```

\

##### Example:

\

```
@Html.ActionLink("Go Back", "Index")
```

##### Html.RouteLink()

##

The RouteLink method renders an element that links to a URL, which can resolve to an [action method](https://www.mindstick.com/forum/160300/how-to-pass-data-to-an-httppost-action-method-in-a-dot-net-core-api), a file, a folder, or some other resource.

Syntax

```
@Html.RouteLink(HtmlHelper, String, Object)
```

**RouteLink** returns an anchor element (an element) that contains the virtual path of the specified action.

**Html.RouteLink()** is equivalent to Html.ActionLink(). You can see below line of code.

<%= Html.RouteLink("Click for Check", new {controller= "Home", action= "Index"}) %>

It is similar to as <a> tag in Html

<a href="/Home/Index">Click for Check</a>

##### Note:

ActionLink will generate the URL to get to an action using the first matching route by action name.

RouteLink will generate the URL to a specific route determined either by name or route values.

##### Url.Action()

It generates a fully qualified URL to an action method.

Example

```
@*link to a controller*@@Url.Action("Home");@*link to an action *@@Url.Action("Home", "Index");@*link to an action and send parameters*@@Url.Action("Edit", "Product", new RouteValueDictionary(new { id = product.Id }));
```

The URL that is returned by this method has a format like the following:

\Home\About

If special characters in the URL must be encoded, use the Encode method. For the previous example, the Encode [method returns](https://www.mindstick.com/forum/12904/main-method-returns-if-submethod-has-stoped-by-return-in-c-sharp) the following URL:

%2fHome%2fAbout

##### Url.RouteUrl()

It generates a fully qualified URL for the specified route values by using a route name and the protocol to use.

Url.RouteUrl method is similar to Url.Action method with some [difference](https://www.mindstick.com/articles/157114/good-news-or-bad-news-and-the-difference-is) that is; Url.Action() is MVC specific (it uses controller and action names), while RouteUrl() is generic is and can be used without MVC (you can have routing in WebForms).

Example

```
@Url.Action("About", "Home", null, Request.Url.Scheme)@Url.RouteUrl("Default", new { Action = "About" }, Request.Url.Scheme)
```

---

Original Source: https://www.mindstick.com/blog/436/optimizing-url-generation-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
