user3692826
user3692826

Reputation: 107

C# Function Encrypt in JavaScript

I want to use Encrypt(myString) from C# in JS code.

Is this possible?

I tried something like that:

In C#

public object IDToUrl(int myNumber)
    {
        return Encrypt(myNumber.ToString());
    }

In JS

var encryptedValue = '<%=IDToUrl(data.id)%>';

but it doesn't work.

Upvotes: 3

Views: 78

Answers (1)

T McKeown
T McKeown

Reputation: 12857

That code would only work if that IDToUrl() method is local to that page. You don't state what view you are using, but I would solve this with a helper class.

Add a Helper class like this:

public static class SomeNameHelper
{
   public static object IDToUrl(int myNumber)
   {
      return Encrypt(myNumber.ToString());
   }
   public static object Encrypt(string s){
      ... whatever code that is required...
   }
}

In js:

var encryptedValue = '<%=SomeNameHelper.IDToUrl(data.id)%>';

Upvotes: 1

Related Questions