Class UrlHelper
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
RequestContextAn 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
RequestContextAn object that contains information about the current request and about the route that it matched.
routeCollection
RouteCollectionA 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
stringThe 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
stringThe name of the action method.
routeValues
objectAn 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
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
stringThe name of the action method.
controllerName
stringThe name of the controller.
routeValues
objectAn 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
stringThe name of the action method.
controllerName
stringThe name of the controller.
routeValues
objectAn 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
stringThe 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
stringThe name of the action method.
controllerName
stringThe name of the controller.
routeValues
RouteValueDictionaryAn 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
stringThe name of the action method.
controllerName
stringThe name of the controller.
routeValues
RouteValueDictionaryAn object that contains the parameters for a route.
protocol
stringThe 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
stringThe name of the action method.
controllerName
stringThe name of the controller.
routeValues
RouteValueDictionaryAn object that contains the parameters for a route.
protocol
stringThe protocol for the URL, such as "http" or "https".
hostName
stringThe 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
stringThe name of the action method.
routeValues
RouteValueDictionaryAn 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
stringThe 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
stringThe 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
stringThe content path.
httpContext
HttpContextBaseThe 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
stringThe route name.
actionName
stringThe action name.
controllerName
stringThe controller name.
protocol
stringThe HTTP protocol.
hostName
stringThe host name.
fragment
stringThe fragment.
routeValues
RouteValueDictionaryThe route values.
routeCollection
RouteCollectionThe route collection.
requestContext
RequestContextThe request context.
includeImplicitMvcValues
booltrue 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
stringThe route name.
actionName
stringThe action name.
controllerName
stringThe controller name.
routeValues
RouteValueDictionaryThe route values.
routeCollection
RouteCollectionThe route collection.
requestContext
RequestContextThe request context.
includeImplicitMvcValues
booltrue 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
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
stringThe route name.
routeValues
RouteValueDictionaryThe 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
stringThe 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
objectAn 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
stringThe 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
stringThe name of the route that is used to generate URL.
routeValues
objectAn 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
stringThe name of the route that is used to generate the URL.
routeValues
objectAn 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
stringThe 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
stringThe name of the route that is used to generate URL.
routeValues
RouteValueDictionaryAn 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
stringThe name of the route that is used to generate URL.
routeValues
RouteValueDictionaryAn object that contains the parameters for a route.
protocol
stringThe protocol for the URL, such as "http" or "https".
hostName
stringThe 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
RouteValueDictionaryAn object that contains the parameters for a route.
Returns
- string
The fully qualified URL.