This documentation is archived and is not being maintained.

ClientScriptManager.GetWebResourceUrl Method

Gets a URL reference to a server resource.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

public string GetWebResourceUrl (
	Type type,
	string resourceName
public String GetWebResourceUrl (
	Type type, 
	String resourceName
public function GetWebResourceUrl (
	type : Type, 
	resourceName : String
) : String
Not applicable.



The type of the server resource.


The name of the server resource.

Return Value

The URL reference to the resource.

Exception typeCondition


The web resource type is a null reference (Nothing in Visual Basic).


The web resource name is a null reference (Nothing in Visual Basic).

- or -

The web resource name has a length of zero.

The GetWebResourceUrl method returns a URL reference to a server resource embedded in an assembly. The returned reference is not URL encoded. Resources can be script files, images, or any static file. You specify the type based on the object that will be accessing the resource. For instance, when using a Page instance to access the resource, you specify the Page type.

A Web resource registered with the page is uniquely identified by its type and name. Only one resource with a given type and name pair can be registered with the page. Attempting to register a resource that is already registered does not create a duplicate of the registered resource.

The GetWebResourceUrl method is used in conjunction with the RegisterClientScriptResource method for accessing resources embedded in assemblies. For more information on using resources in applications, see ASP.NET Web Page Resources Overview.

The following code example demonstrates the use of the GetWebResourceUrl method. The type parameter in this example is set to the type of class in the assembly containing the resource.

<%@ Page Language="C#"%>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">
  public void Page_Load(Object sender, EventArgs e)
    // Define the resource name and type.
    String rsname = "script_include.js";
    Type rstype = typeof(ClientScriptResourceLabel);
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Write out the web resource url.
    ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname);

    // Register the client resource with the page.
    cs.RegisterClientScriptResource(rstype, rsname);

<html xmlns="" >
    <title>ClientScriptManager Example</title>
     <form    id="Form1"
     The web resource path is 
     <span  id="ResourcePath"
     <br />
     <br />
     <input type="text" 
            id="Message" />     
     <input type="button" 
            value="ClientClick" />

The following code example demonstrates how to programmatically apply the WebResourceAttribute metadata attribute to mark the assembly for the resources that will be served.

using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;

[assembly: WebResource("script_include.js", "application/x-javascript")]
namespace Samples.AspNet.CS.Controls
	[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
	public class ClientScriptResourceLabel
		// Class code goes here.


This example requires a JavaScript file named Script_include.js, with the following contents:

function DoClick() {Form1.Message.value='Text from resource script.'}

Compile the Script_include.js file as a resource in the assembly that contains the Samples.AspNet.CS.Controls.ClientScriptResourceLabel class.

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0