Table of Contents

Class UrlHelper

Namespace
System.Web.Mvc
Assembly
System.Web.Mvc.dll

Contains methods to build URLs for ASP.NET MVC within an application.

public class UrlHelper
Inheritance
UrlHelper
Inherited Members

Constructors

UrlHelper()

Initializes a new instance of the UrlHelper class.

public UrlHelper()

UrlHelper(RequestContext)

Initializes a new instance of the UrlHelper class using the specified request context.

public UrlHelper(RequestContext requestContext)

Parameters

requestContext RequestContext

An object that contains information about the current request and about the route that it matched.

UrlHelper(RequestContext, RouteCollection)

Initializes a new instance of the UrlHelper class using the specified request context and route collection.

public UrlHelper(RequestContext requestContext, RouteCollection routeCollection)

Parameters

requestContext RequestContext

An object that contains information about the current request and about the route that it matched.

routeCollection RouteCollection

A collection of routes.

Properties

RequestContext

Gets information about an HTTP request that matches a defined route.

public RequestContext RequestContext { get; }

Property Value

RequestContext

The request context.

RouteCollection

Gets a collection that contains the routes that are registered for the application.

public RouteCollection RouteCollection { get; }

Property Value

RouteCollection

The route collection.

Methods

Action()

Generates a string to a fully qualified URL to an action method.

public virtual string Action()

Returns

string

A string to a fully qualified URL to an action method.

Action(string)

Generates a fully qualified URL to an action method by using the specified action name.

public virtual string Action(string actionName)

Parameters

actionName string

The name of the action method.

Returns

string

The fully qualified URL to an action method.

Action(string, object)

Generates a fully qualified URL to an action method by using the specified action name and route values.

public virtual string Action(string actionName, object routeValues)

Parameters

actionName string

The name of the action method.

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

Returns

string

The fully qualified URL to an action method.

Action(string, string)

Generates a fully qualified URL to an action method by using the specified action name and controller name.

public virtual string Action(string actionName, string controllerName)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

Returns

string

The fully qualified URL to an action method.

Action(string, string, object)

Generates a fully qualified URL to an action method by using the specified action name, controller name, and route values.

public virtual string Action(string actionName, string controllerName, object routeValues)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

Returns

string

The fully qualified URL to an action method.

Action(string, string, object, string)

Generates a fully qualified URL to an action method by using the specified action name, controller name, route values, and protocol to use.

public virtual string Action(string actionName, string controllerName, object routeValues, string protocol)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

protocol string

The protocol for the URL, such as "http" or "https".

Returns

string

The fully qualified URL to an action method.

Action(string, string, RouteValueDictionary)

Generates a fully qualified URL to an action method by using the specified action name, controller name, and route values.

public virtual string Action(string actionName, string controllerName, RouteValueDictionary routeValues)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

Returns

string

The fully qualified URL to an action method.

Action(string, string, RouteValueDictionary, string)

Generates a fully qualified URL for an action method by using the specified action name, controller name, route values, and protocol to use.

public virtual string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

protocol string

The protocol for the URL, such as "http" or "https".

Returns

string

The fully qualified URL to an action method.

Action(string, string, RouteValueDictionary, string, string)

Generates a fully qualified URL for an action method by using the specified action name, controller name, route values, protocol to use and host name.

public virtual string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName)

Parameters

actionName string

The name of the action method.

controllerName string

The name of the controller.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

protocol string

The protocol for the URL, such as "http" or "https".

hostName string

The host name for the URL.

Returns

string

The fully qualified URL to an action method.

Action(string, RouteValueDictionary)

Generates a fully qualified URL to an action method for the specified action name and route values.

public virtual string Action(string actionName, RouteValueDictionary routeValues)

Parameters

actionName string

The name of the action method.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

Returns

string

The fully qualified URL to an action method.

Content(string)

Converts a virtual (relative) path to an application absolute path.

public virtual string Content(string contentPath)

Parameters

contentPath string

The virtual path of the content.

Returns

string

The application absolute path.

Encode(string)

Encodes special characters in a URL string into character-entity equivalents.

public virtual string Encode(string url)

Parameters

url string

The text to encode.

Returns

string

An encoded URL string.

GenerateContentUrl(string, HttpContextBase)

Returns a string that contains a content URL.

public static string GenerateContentUrl(string contentPath, HttpContextBase httpContext)

Parameters

contentPath string

The content path.

httpContext HttpContextBase

The http context.

Returns

string

A string that contains a content URL.

GenerateUrl(string, string, string, string, string, string, RouteValueDictionary, RouteCollection, RequestContext, bool)

Returns a string that contains a URL.

public static string GenerateUrl(string routeName, string actionName, string controllerName, string protocol, string hostName, string fragment, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues)

Parameters

routeName string

The route name.

actionName string

The action name.

controllerName string

The controller name.

protocol string

The HTTP protocol.

hostName string

The host name.

fragment string

The fragment.

routeValues RouteValueDictionary

The route values.

routeCollection RouteCollection

The route collection.

requestContext RequestContext

The request context.

includeImplicitMvcValues bool

true to include implicit MVC values; otherwise false.

Returns

string

A string that contains a URL.

GenerateUrl(string, string, string, RouteValueDictionary, RouteCollection, RequestContext, bool)

Returns a string that contains a URL.

public static string GenerateUrl(string routeName, string actionName, string controllerName, RouteValueDictionary routeValues, RouteCollection routeCollection, RequestContext requestContext, bool includeImplicitMvcValues)

Parameters

routeName string

The route name.

actionName string

The action name.

controllerName string

The controller name.

routeValues RouteValueDictionary

The route values.

routeCollection RouteCollection

The route collection.

requestContext RequestContext

The request context.

includeImplicitMvcValues bool

true to include implicit MVC values; otherwise false.

Returns

string

A string that contains a URL.

HttpRouteUrl(string, object)

Generates a fully qualified URL for the specified route values.

public virtual string HttpRouteUrl(string routeName, object routeValues)

Parameters

routeName string

The route name.

routeValues object

The route values.

Returns

string

A fully qualified URL for the specified route values.

HttpRouteUrl(string, RouteValueDictionary)

Generates a fully qualified URL for the specified route values.

public virtual string HttpRouteUrl(string routeName, RouteValueDictionary routeValues)

Parameters

routeName string

The route name.

routeValues RouteValueDictionary

The route values.

Returns

string

A fully qualified URL for the specified route values.

IsLocalUrl(string)

Returns a value that indicates whether the URL is local.

public virtual bool IsLocalUrl(string url)

Parameters

url string

The URL.

Returns

bool

true if the URL is local; otherwise, false.

RouteUrl(object)

Generates a fully qualified URL for the specified route values.

public virtual string RouteUrl(object routeValues)

Parameters

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

Returns

string

The fully qualified URL.

RouteUrl(string)

Generates a fully qualified URL for the specified route name.

public virtual string RouteUrl(string routeName)

Parameters

routeName string

The name of the route that is used to generate URL.

Returns

string

The fully qualified URL.

RouteUrl(string, object)

Generates a fully qualified URL for the specified route values by using a route name.

public virtual string RouteUrl(string routeName, object routeValues)

Parameters

routeName string

The name of the route that is used to generate URL.

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

Returns

string

The fully qualified URL.

RouteUrl(string, object, string)

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

public virtual string RouteUrl(string routeName, object routeValues, string protocol)

Parameters

routeName string

The name of the route that is used to generate the URL.

routeValues object

An object that contains the parameters for a route. The parameters are retrieved through reflection by examining the properties of the object. The object is typically created by using object initializer syntax.

protocol string

The protocol for the URL, such as "http" or "https".

Returns

string

The fully qualified URL.

RouteUrl(string, RouteValueDictionary)

Generates a fully qualified URL for the specified route values by using a route name.

public virtual string RouteUrl(string routeName, RouteValueDictionary routeValues)

Parameters

routeName string

The name of the route that is used to generate URL.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

Returns

string

The fully qualified URL.

RouteUrl(string, RouteValueDictionary, string, string)

Generates a fully qualified URL for the specified route values by using the specified route name, protocol to use, and host name.

public virtual string RouteUrl(string routeName, RouteValueDictionary routeValues, string protocol, string hostName)

Parameters

routeName string

The name of the route that is used to generate URL.

routeValues RouteValueDictionary

An object that contains the parameters for a route.

protocol string

The protocol for the URL, such as "http" or "https".

hostName string

The host name for the URL.

Returns

string

The fully qualified URL.

RouteUrl(RouteValueDictionary)

Generates a fully qualified URL for the specified route values.

public virtual string RouteUrl(RouteValueDictionary routeValues)

Parameters

routeValues RouteValueDictionary

An object that contains the parameters for a route.

Returns

string

The fully qualified URL.