Tuesday, July 2, 2019

ASP.NET Core What is it ?


ASP.Net Core is the latest trend of building modern, cloud-based, Internet-connected applications which introduce with .NET Framework 4.6. on words. and its a light-weight, open-source and cross-platform and it able to runs on the top of .NET Core or full .NET Framework having the version higher than 4.6.

Advantages of using Asp.Net Core
  • Open source
  • Cross platform framework which runs on macOS, Linux, and Windows.
  • Runs on the top of .NET Core or full .NET Framework having version 4.6+.
  • A unified framework for building web UI and web APIs.
  • Built-in dependency injection.
  • New light-weight and modular HTTP request pipeline.
  • Ships entirely as NuGet packages.
  • Side-by-side versioning when targeting .NET Core.
  • Built-in supports for SPA using client-side frameworks like Angular, React, Vue etc.
  • Supports IIS, Nginx, Apache, Docker, or self-host deployment.
 The Architecture of ASP.Net Core


ASP.NET Core has a number of architectural changes that result in a much leaner and modular framework. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages. This allows you to optimize your app to include just the NuGet packages you need. The benefits of a smaller app surface area include tighter security, reduced servicing, improved performance, and decreased costs in a pay-for-what-you-use mod


Please note that ASP.NET Core and .NET Core are not the same. They are different, just like ASP.NET and .NET Framework are different.

Lets start the sample project 

here i am using VS 2017.

select the mvc template to follow the mvc architecture.


here is the structure of the project 
in the solution explorer the dependencies are the project references and it has two parts Nuget and SDK. since ASP.NET Core is not based on System.Web.dll its used NuGet packege. and We can see the SDK and inside SDK, we can see the “Microsoft.NETCore.App”. It is a target to .NET Core framework.
 Program.cs class
Program class is the main part of the ASP.NET Core. It contains static method and also beginning pointing of the application. This main method creates a host, builds a host, and runs the host.
 Startup Class
Startup class is a special class and it does not have any inheritance, interface, and over loading. It is mainly used to start the application
Time to say Hello world !
Add controller to project right click on controllers folder in solution explorer and Add -> Controller and name it as HelloWorldController.cs after

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace MyCoreApp1.Controllers
{
    public class HelloWorldController : Controller
    {
        //public IActionResult Index()
        //{
        //    return View();
        //}
        public string Index()
        {
            return "This is the index action";
        }

        public string Welcome()
        {
            return "This is the Welcome action";
        }
    }
}
 
after this its working same as default .net mvc routing when call https://localhost:44365/HelloWorld its invoke index action method and if you call https://localhost:44365/HelloWorld/Welcome its invoke the Welcome action method. 
and if you want to apply default URL just change the routing format in startup.cs as per the  common MVC routing logic

/[Controller]/[ActionName]/[Parameters]
 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=HelloWorld}/{action=Welcome}/{id?}");
            });
        }
Add a View
 
 
 
Right click on the Views folder, and then Add > New Folder and name the folder HelloWorld.

Right click on the Views/HelloWorld folder, and then New Item -> Web ->Razor View 
name it as index.cshtml

and insdie the  index.cshtml
add the following code
 
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>Hello from our Index View Template!</p>
 
 
  and in the HellowWorld HelloWorldController.cs change the index method as follow
  
        public IActionResult Index()
        {
            return View();
        }


after this the index.cshtml page load secussfully. 
from the next post ill connect the ASP.Net core app connected to SQL database with entity freamwork.





Tuesday, September 4, 2018

C# TryParse() Safe convertion.

TryParse() - C# best practice

TryParse() is introduce In .Net framework 2.0 for safe conversion in string to any numismatic value. and its a replacement method of Parse which exist in earlier version of .net framework.

TryParse() has many advantages ;
  • Safe to convert string to many data types (int,decimal,float,byte,Boolean,double)
  • Not throw an exception if the conversion fails.
  • Return boolean if the conversion fails.
  • Handle the null conversion. 

Why to use TryParse() instead of Parse();

Parse method will throw exception - ArgumentNullexception or FormatException or OverflowException, if the string value cannot be converted to the respective data type. so it will impact the system performance.

Explore the TryParse()
  
1:  int parsedResult;   
2:  int.TryParse("125A", out parsedResult);  
out put is

parsedResult =0


With more validation
       

string strTextData = "125A";
int parsedResult; 

if (int.TryParse(strTextData , out parsedResult))
{
   return "Valid Data";

}
else
{
 return "Invalid Data";
}


       
 

Friday, June 1, 2018

Enable remote Debugging for Android Device in Chrome

Here is simple steps to enable remote debugging for Android Device in Chrome.



you can easily debug your website contain or hybrid web app in android device in chrome remote debugging option. this feature available from Android ice cream sandwich (4.0) on words.

On your Android device.
  1. plug you android device to computer.
  2. Enabled developer option.
    1.   Find software build number by 
                   in most samsung devices  go to   Settings > About phone > Build number or
                   Settings > About phone > Software information > Build number
                   tap 7 time on the Build number.
                   After that the Developer options will available under the settings menu


    2. USB Debugging - Settings > Developer options > Check “USB debugging”.

On your Computer
  1.  install ADBPlugin  Chrome Extension from the webstore 
    1. Click on the Android icon in your Chrome menu and select “Start ADB”

                             or
    download ADB (Android debug bridge ) here  and extract the folder and open command prompt then move the  "platform-tools" folder and give the adb usb command.

  2. Open chrome inspect mode and enable the remote device.

 3. now you can see the device is connected
4. now in your android device open chrome browser and type the url need to inspect and then click the inspect icon.

.

Thursday, May 31, 2018

Introduction To Xamarin

Ok lets start it this way.. iam so new to Xamarin. previously I spell it as X - marin but peoples call it samaring ;-) .

so What is Xamarin ?


simply its a common mobile application development platform for IOS , Android and windows phone. that means developers able to sheared their single code base with multi mobile platform.


How it Works !
as its Microsoft no need to worry much. you can use Xamarin studio or visual studio for windows or mac. (here i start with VS 2015)

Why Xamarin ?

  • Xamarin is based in native platform so the performance of the application is  high.
  • All the native based APIs are available in the xamarin.
  • UI design are focus in the native platform. the user experience is almost same for the native android/ios app and the xamarin app
  • Developer need to know one language to cater many platform.
Xamarin Native Vs Xamarin Forms

There are two type of Xamarin developments.




Xamarin Native is the traditional stage of Xamarin app development and its simply based on the mobile platform and developer need to maintain separate UI for each platform

Xamarin Native
Pros

  • Create one UI per platform.
  • Easy ability to adjust the platform specific UI features and components.
  • Get the maximum out of your UI.

Cons

  • Cross platform development process is much slower.
  • You need to learn all native UI frameworks.
  • You may result in having duplicate code (as you describe almost the same UI in three different platform specific ways).    
Xamarin Form
Xamarin form is latest approch of xamarin and it enable the developer to share UI and code base among the multiple platform. 

Pros

  • Create one UI for all platforms.
  • Use basic components that are available on all platforms (like Buttons, Textfields, Spinners etc.).
  • No need to learn all the native UI frameworks.
  • Fast cross platform development process.
  • Custom native renders give you the ability to adjust the appearance and feeling of controls.
Cons

  • It's still a new framework and still contains bugs.
  • Less resources.
  • It's sometimes slower than accessing the native controls directly.
  • Custom native renders have boundaries and are poorly documented.