PageMethods is not working (Solved)

PageMethods is a JavaScript class that you can use to call a server-side function. It’s basically an AJAX call which works asynchronously. In .NET world, you can use PageMethods to call a C# code-behind function. If you think that PageMethods is not working no matter what you do, keep reading.

For PageMethods to work, you need a JavaScript function on the client-side and a WebMethod function on the server-side.

Sample JavaScript code (Put it in asp:content section of Default.aspx):

<script type="text/jscript">

   function callCodeBehind() {
		PageMethods.GetMessage("test string", onSuccess, onFailure);

   function onSuccess(result, usercontext, methodname) {

   function onFailure(error, usercontext, methodname) { alert("failed: "+ error.get_message()); }


Sample C# code (code-behind file – Default.aspx.cs):

public partial class Welcome : System.Web.UI.Page
	protected void Page_Load(object sender, EventArgs e)


	protected static void GetMessage(string val)
		return "doingit";

A button to call the JavaScript function (in Default.aspx)

<button onclick="callCodeBehind()">Click me</button> 

ScriptManager (in Site.Master – note that EnablePageMethods is set to True):

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">

Having issues with your AJAX call as part of Google reCAPTCHA implementation? Check this post out.

What to do if PageMethods is not working

If you have the code above, your project should be working. In exceptional cases, you may not receive the return value. In my case, the issue was the post-back that was happening after clicking the button.

The reason behind the post-back was that friendy URL setting. As soon as I disabled it, my PageMethods function started working.

In order to disable friendly URL feature, remove or comment out the line below in your RouteConfig file.

Solved PageMethods is not working issue
Successfully returned the value

Are you losing data after a post-back? Here is what to do.


Cannot perform CAS Asserts in Security Transparent methods (Solved)

You may come across this error message while trying to use a third-party library in your .NET application: “Cannot perform CAS Asserts”. As the message implies, this issue is about the security mechanism that protects your system from unreliable components.

Here is the entire error message:

System.InvalidOperationException: Cannot perform CAS Asserts in Security Transparent methods

What is CAS (Code Access Security)?

CAS is a security mechanism provided by .NET Framework to protect your system against malicious code blocks and components.

The .NET Framework provides a mechanism for the enforcement of varying levels of trust on different code running in the same application called Code Access Security (CAS).

Microsott Docs

Why we need CAS?

Users obtain code from many sources, some of which might be unreliable; code can contain bugs or vulnerabilities that enable it to be exploited by malicious code; and code sometimes does things that the user does not know it will do. As a result, computer systems can be damaged and private data can be leaked when cautious and trustworthy users run malicious or error-filled software.

Microsott Docs
How to solve "Cannot perform CAS Asserts" error
Slide source

Root Cause

The reason you receive “Cannot perform CAS Asserts” error is that your application is trying to use an external library that is not trusted by CAS. It happens especially when you upgrade .NET Framework version in your development machine. In this case, you should update your code as per the new CAS requirements come with the new .NET Framework version.

For more information about major CAS changes in .NET Framework 4.0: Link 1, link 2

Receiving Schannel 10013 error? Check this post out.

How to solve “Cannot perform CAS Asserts” error

Solution 1: Full-trust

Making your application a “full-trust” application solves this issue unless the application is marked as security-transparent. However, many hosting companies don’t allow applications run on “full-trust”.

Solution 2: GAC

Another way to solve this issue is that adding the external DLLs into GAC (Global Assembly Cache) by using Gacutil.exe. Check this article out for instructions to install an assembly into GAC. Gacutil.exe comes with Windows SDK and Visual Studio.

Solution 3: Code changes

Changing your application’s code and making it compatible with the new CAS implementations is the best solution in the long-term. Here is an article to guide you through the changes you should make in your code.

How to find out the Public Key Token for an assembly (DLL) file?

In your ASP.NET project, you may need to use public key token of an assembly file while adding it as a reference. Here is an example reference declaration from project.csproj file:

<Reference Include="Oracle.DataAccess, Version=, Culture=neutral, PublicKeyToken=89b483f429c47342, processorArchitecture=x86">

As specified in Microsoft’s documentation, publicKeyToken field is optional. However, it is recommended to use it for a strong mapping of your assemblies.

Before going further; What is the public key token? A good description:

The public key token is a small number which is a convenient “token” representing a public key. Public keys are quite long; the purpose of the public key token is to let you refer to keys without saying the whole key.

The question comes up at this point: How to find out this value?

Find public key token value of an assembly file

The easiest way of finding out this value is that using Strong Name Tool. It is used for assembly signing, key management, signature generation and verification. Strong Name Tool is automatically installed with Visual Studio.

Follow the steps below to get values from your assembly file:

  1. Open Visual Studio Command Prompt (Start > Visual Studio > Visual Studio Tools > Developer Command Prompt)
  2. Run the command below. It will display both public key token and public key. Make sure to replace C:\Temp\Oracle.DataAccess.dll with the full path of your DLL file
sn -Tp C:\Temp\Oracle.DataAccess.dll

Assembly public key token

If you only want to see the public key token, you can only use -T parameter.

sn -T C:\Temp\Oracle.DataAccess.dll

Even if you configure everything correctly, you may run into “Could not load file or assembly” issue. Read about Assembly Binding Logging to troubleshoot these kind of issues.

Alternative Way (PowerShell)

If you don’t have Visual Studio installed, you can display the same information by using a PowerShell command. Follow the steps below to to view this information.

  1. Open PowerShell (Start > Search > PowerShell)
  2. Run the command below. Make sure to replace C:\Temp\Oracle.DataAccess.dll with the full path of your DLL file

DLL public key token powershell