Thursday, August 31, 2017

Base64 encoding and decoding(btoa & atob)

Base64 is a group of similar binary-to-text encoding schemes that represent binary data in an ASCII string format by translating it into a radix-64 representation.

In JavaScript there are two functions respectively for decoding and encoding base64 strings:
atob()
btoa()

In these APIs, for mnemonic purposes, the "b" can be considered to stand for "binary", and the "a" for "ASCII"

The atob() function decodes a string of data which has been encoded using base-64 encoding.

Conversely, the btoa() function creates a base-64 encoded ASCII string from a "string" of binary data.

Both atob() and btoa() work on strings

Syntax
var encodedData = scope.btoa(stringToEncode);

Parameters
stringToEncode
A string whose characters each represent a single byte of binary data to be encoded into ASCII.
Return value
A string containing the Base64 representation of stringToEncode.

Example

var encodedData = window.btoa('Hello, world'); // encode a string
var decodedData = window.atob(encodedData); // decode the string

how to call javascript function in c#

Home.aspx.cs

protected void BtnLogin_click(object sender,eventargs e)
{

//when i click this button i need to call javascript function
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(@"<script language="'javascript'">");
            sb.Append(@"JavaScriptFunctionName();");
            sb.Append(@"</script>");
       System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "JSName",     sb.ToString(), false);

}


Home.aspx

<script type="text/javascript">
function JavaScriptFunctionName()
{
 alert('Called JS function.');
}
</script>



Thursday, August 24, 2017

To Get HTTP-specific information about the current request.

HttpContext

ASP.NET core infrastructure is built around HttpApplication class
which has a reference to System.Web.HttpContext (System.Web) class’s instance in its Context property.

The problem with this is that it has no base class and isn't virtual,
and hence is unusable for testing (cannot mock it).
It's recommended to not pass it around as function arguments, instead pass around variables of type HttpContextBase.

HttpContextBase

This is the (new to c# 3.5) replacement to HttpContext.
Since it is abstract, it is now mockable.
The idea is that your functions that expect to be passed a context should expect to receive one of these.
It is concretely implemented by HttpContextWrapper.

ASP.NET MVC on the other hand uses System.Web.HttpContextBase.Controller class has Context property of type System.Web.HttpContextBase.
Idea behind introducing the new HttpContextBase instead of HttpContext is to allow unit-testing due to HttpContextBase is an abstract class.

HttpContextWrapper

Encapsulates the HTTP intrinsic object that contains HTTP-specific information about an individual HTTP request.

Also new in C# 3.5 - this is the concrete implementation of HttpContextBase.
To create one of these in a normal webpage, use new HttpContextWrapper(HttpContext.Current).

The idea is that to make your code unit-testable,
you declare all your variables and function parameters to be of type HttpContextBase,
and use an IOC framework .
In normal code, castle is to inject the equivalent of 'new HttpContextWrapper(HttpContext.Current)',
whereas in test code you're to be given a mock of HttpContextBase.


But sometimes we may need to convert HttpContext into HttpContextBase and vice versa.
E.g. when we have common logic which is being used from HttpApplication and from MVC controllers.

Getting HttpContext from HttpContextBase’s instance is easy:
HttpContext httpContext = httpContextBase.ApplicationInstance.Context;

To get HttpContextBase from HttpContext we have to wrap it in HttpContextWrapper:
HttpContextBase abstractContext = new System.Web.HttpContextWrapper(context);

Change default Port on Next.js app

 If any other app or process is running on port 3000 , you will get this error in your terminal Port 3000 is already in use. error Command f...