Step by step migration of ASP.NET Core 2.2 to 3.1

In order to upgrade/migrate asp.net core version to 3.1 following steps to be perform.
Download and install Visual Studio 2019 version 16.4 or higher.Download and install .NET Core 3.1  https://dotnet.microsoft.com/download/dotnet-core/3.1Upgrade the projects of solution to .NET Core 3.1 , because ASP.NET Core 3.1 requires it.Update existing Nuget packages to a version compatible with ASP.NET Core 3.1 in each projectUse IHost interface from a IHostBuilder instead of building and running a IWebHost from a IWebHostBuilder in Program.cs public static IHostBuilder CreateWebHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseKestrel() .UseSerilog() .UseStartup<Startup>(); }) Change AddMvc method has been replaced by AddControllers in Startup.cs of each projects.In Startup.cs,UseMvc method to be replaced by…

Call server side methods asynchronously without having a postback using callback in asp.net

Callback is another fabulous way to communicate with server for light weight tasks. While working in ASP.NET, sometimes we need to call server side methods asynchronously without having a postback. Sometimes it is either a full page postback or partial page postback.

To Implement callback in asp.net following interfaces to me implement in the asp.net page

  • ICallback
  • ICallbackEventHandler

Let us first implement ICallbackEventHandler to call the server side method asynchronously step-by-step. The following are the definition of two methods which we need to implement. RaiseCallbackEvent method invoke through JavaScript function.


public void RaiseCallbackEvent(string eventArgument)

{

  //populate Customer object to return

  Customer customer = new Customer();

  customer.Name = "Muhammad Adnan";

  customer.Age = 24;

  //javascript serialization of Customer object

  System.Web.Script.Serialization.JavaScriptSerializer jss;

  jss = new System.Web.Script.Serialization.JavaScriptSerializer();

 

  //stringbuilder to contain serialized customer object

  System.Text.StringBuilder sbCustomer = new System.Text.StringBuilder();

  jss.Serialize(customer, sbCustomer);

 

  jsonResult = sbCustomer.ToString();

}

 

public string GetCallbackResult()

{

  return jsonResult;

}

In Page_Load or Page_Init event the following statements are used to register client side methods.CallServer(arg, context), as the name implies, would use call/raise server side method which was RaiseCallbackEvent string eventArgument). ReceiveServerData(arg, context) would get the result through arg parameter by GetCallbackResult().

protected void Page_Load(object sender, EventArgs e)

{

ClientScriptManager scriptMgr = Page.ClientScript;

String cbReference = scriptMgr.GetCallbackEventReference(this, "arg", 

"ReceiveServerData", "");

String callbackScript = "function CallServer(arg, context) {" + cbReference + "; }";        

cm.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);

}

Client side ICallback Implementation

<script language=javascript type=text/javascript>

function ReceiveServerData(arg, context)

{

  alert(arg); //just to show output

} 

function CallSrv()

{

  CallServer('get customer', '');

}

</script>

<input type="button" value="get customer" onclick="CallSrv()" />

I have used JSON based JavaScript serialization to return results in JavaScript's easily parseable format.

Suppose we have the following class whose object we need to return to JavaScript function through JavaScript serialization.

public class Customer

{

  public string Name;

  public int Age;

}

using above steps we can get Asynchronously output within a millisecond and without Postback.
 

Comments

Popular posts from this blog

Restore the lost focus of Auto post back controls in asp.net update Panel control

Add Comma automatically while entering amounts in textbox using Javascript

Devexpress Datebox date formatting in angular 6 with example