Saturday, 26 May 2018

OAuth Integration with MVC WebApi

What is OAuth

oAuth stands for Open Authorization. It helps to access the resource in secured way , when the resource owner want it to share to any third party provider.
Like you logged in to the twitter and now you want to send a friend request to all your google address book user. So you can login securely to the google and than google will authenticate and share the entire address book to twitter and that you can use to send the friend request.

How it Work

User has logged into as is user credential  into seeker Resource (SR) now he want to get the details from third party resource(RO)  where he already have account. He has to provide the credentials than third party resource owner (RO) will allocate the security token. Then Seeker Resource (SR) will send the Security token to the resource Owner(RO), and it will give the details of resource what Seeker Resource want.

Add a startup.cs class to your application

Startup page is mostly using to configuration for startup of the application when you are configuration OWIN or the OAuth authorization.

There are two what you can directly add it and  as a class file and decorate it as. OWinStartup:

  [assembly: OwinStartup(typeof(WebApiOAuth_1.App_Start.Startup))]

Or you can create startup.cs through the Visual Video by using the below process.
a.       Rt click on App_Start à Add à Class

XMLformatting in c#

Format the minify XML into readable way which help to view it properly and easy to read in C#. You can make your simple xml formatter by using the below few lines of code in C#.
static void Main(string[] args)
                string xml = @"<?xml version='1.0' encoding='UTF - 8' ?><employees><employee><id>1</id><firstName>Leonardo</firstName>
            </employee></employees> ";

Thursday, 24 May 2018

Could not load file or assembly 'Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed'

Could not load file or assembly 'Newtonsoft.Json, Version=, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Sunday, 20 May 2018

Binding DropDownList in MVC

In MVC we have 2 ways to bind the dropdown is strongly type and nomal way.
There are various ways that you can bind the dropdownlist in MVC.

1.       Binding Dropdownlist  using the view bag in MVC
Binding SelectedList to Dropdown

  List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "Male", Value = "Male", Selected = false });
            items.Add(new SelectListItem() { Text = "Female", Value = "Female", Selected = true });

            SelectList selectList = new SelectList(items, "Value", "Text");

            ViewBag.Gender = selectList;

Binding it in HTML, like below
  @Html.DropDownList("Gender", ViewBag.Gender as SelectList)

Will get output as below.

Friday, 18 May 2018

Format date in ui-grid cellTemplate

This sample is using when you are implementing MVC with Angular 1.x and Angular Ui Grid.
Formatting of date field will be appearing like:

Sunday, 13 May 2018

Handling Exception in Global.asax in MVC

Instead of writing the try and catch throughout the application, global.asax , application_error method is the common place to handle all the error and based on the type of exception it will redirect to the page.

Tuesday, 8 May 2018

Selecting the active link in MVC

Based on selection or opening of the controller action the link will select in MVC like below.

There should you need to provide the css dynamically which is based on the action. You need to create the CSS style.
.activelink {
    color: #ffffff;
    background-color: #de0301;
    border-radius: 10px;
    margin-left: 4px;
If you see the below code there we are checking the (ViewContext.RouteData.Values["Controller"].ToString() == "Home"