The Dummies’ Guide to How to solve Problems with Extension Method

I am pretty much sure that, You have used Extension method in your program. Did you think to create your custom Extension method ?

If not then this article help you to create your own Extension method step by step.

Before jumping to direct practice let’s understand first what is extension method. So, an Extension method is created to simplify the code. It is a static method which represent as instance method. It accepts “this” keyword as a parameter.

If you see below example image you are not going to surprise as you have used this type of method.

extension_Method_1-1

Now, let me tell you more about the Extension method. As you now aware it is a static method & it accept “this” as a parameter so just need to add a “.” dot symbol with variable and all the existing method will be appear.

The question come to our mind how to create our own Extension method. So, it is simple and easy. We need to create a static class first and then that static class we can create our own Extension method.

extension_method2-1

Once you added a class in your solution specify the class as a static class and a static method in the class. In below example I am using static method with name  “MyStringCharacterLength”  if you see the below snap you will find in the parameter we are using this which is must for an extension method. We are not doing any fancy with this method it is a simple logic of calculating the length of the string.

Extension_Method_3

Now once the extension method is created we can use this method using by a “.” dot symbol. Isn’t it easy let me show you the code.

extension_Method_4

When we run this code it will return the length of the string which provided as a input.

I hope a basic now you have a basic understanding of extension method. You can create your various method to use this and reduce your efforts in cases where you have to write similar logic again and again.

I recommend below site from where you can download existing Extension method and  use it.

http://extensionmethod.net/

I hope you have a clear view of extension method now.

Enjoy!!

LESS but it is more–CSS Preprocessor with ASP.NET

 

Now a days a “ CSS  pre processor “ is a very famous buzz word.  Although I am a web developer but still I am not always interested in writing CSS  don’t know why.

When I heard about css pre processor then I was curious to know what it exact about.

So let me share my thought here about CSS Preprocessor.

So a  CSS pre processor is a way to extend the CSS language and variables, functions and programming  stuff.

As a developer you will be more happy to have pre processor because it allows you to write code and also reduce the amount of css up to some extent.

There are many CSS  pre processor are exists in the market  like LESS, SASS,

Stylus,clay and many more.

Now Let me share here how to use LESS.css in your ASP.NET code.

Step 1:-  Add  Nuget package Less.js

Less_Nuget_Package

once you added this package in your script folder you will get 2 new java script file with prefix less as shown below in figure

Less_Script

Step 2:- Now right click on content folder and add a new item which is LESS style sheet. This file will have extension .less

 

Less

Step 3:-  Now you can write your LESS related code as shown in below snap

lesscss

If you see above snap I declared  variable like @back-color , @font-color etc

and used in body.

Step 4: Now  add a web page and write following lines to enable LESS css.

<link href="Content/MyLessCss.less" rel="stylesheet/less" />
   <script src="Scripts/less-1.5.1.min.js"></script>

Test_Less_Page

step 5:- Now in web.config we need to add following mime type

Webconfig_less

Or you can add this from IIS.

Step 6: Now we are good to go and can run  it

sample_Less__page

So we get the color according to variable which we declared.

It means we can play with the variables and can write the less code in programming way.

I hope this post will give a step to go further and I am sure you will deep dive in CSS pre processor soon.

Enjoy !!!

RJ !!

How easy to reduce efforts in retesting a asp.net web application?

 

Most of the time when you write code for a web application you need to test it again and again. You have to run a whole flow of positive or negative test cases to check whether your application is working or not.

Now suppose your application have 20 pages and you need to traverse each individual page and may be perform some operation and on each change of your development you need to perform same thing.

sometimes, this thing will irritate you and you will skip testing which may be harmful because most of the time it happens when you skipped something Smile you took a deep breath  and at that moment your application crashed Sad smile.

So , as a responsible developer we do not have to skip any test scenario but next question is how to reduce this effort which you feel boring sometime.

I can say we can reduce this effort by a installing a simple  Firefox add-in which is absolutely free. The add in “Selenium add in”

you can download it from https://addons.mozilla.org/en-US/firefox/addon/selenium-expert-selenium-ide/

once you have installed this you will find the selenium icon near by search bar as shown below

selenium_icon

when you click this icon you will get a selenium interface as shown below

selenium_interface

The basic concept of this tool is what ever you will run in browser it has capability of recording those steps so it is just play button and pause button game, no rocket science. You can easily run and use this.

I am sure it will reduce your testing effort and when when selenium ide will play your test cases you can take a deep breath and enjoy your coffee and snacks.

I hope this tip may be helpful to you somewhere.

Happy Holidays.

Regards,

RJ

MVC from Beginner to advance session by Indiandotnet

 

Dear All,

Thanks for attending session on 30th Nov 2014. I would like to say a big thanks to Abhishek Sapkale also who delivered the session and share knowledge with us.

We tried to  do go live in this seminar and some people joined also and enjoyed the live streaming.

Those who were not able to attend the session due to some reason, they can see the videos of session from below links

https://www.youtube.com/watch?v=gDYLJrOUJ7w

https://www.youtube.com/watch?v=UIPauB1Ixzc

https://www.youtube.com/watch?v=UIPauB1Ixzc

You can also watch presentation at slide share

http://www.slideshare.net/Indiandotnet/slideshelf

 

I hope it might help you.

Thanks

Rj !!!

How to create Web API ?

Dear Friends,

In Last article https://indiandotnet.wordpress.com/2014/02/23/what-is-web-api/ 

We have discussed what is Web API. Now in this article we will see how to create web API. So with out wasting much time lets start visual studio (In my case I am running VS 2013 web express).

So in my example I am creating a web API project for Friends. this API should return either all the friends or specific friend according to Id so lets start.

Step 1: Create a  new web project when you click on web project you will get following option. Select Web API option as shown in below screen shot.

project-WebAPi

You will get default project as below with defined folder structure

defaultProject

The main point here is to remember Model class should come in model folder & Controller class should come in controller folder

Step 2:- Once you click on Web API option project will open. Now Create a model (a simple class) with name Friend. this model class have basic friend’s property like his firstName, LastName,Id etc. as shown in below fig

FriendModelClass

Step 3:- Now create  a Friend controller so for this just add new item which is controller as shown in below fig

AddWebAPIController

In the above option I have selected Web API 2 controller Empty template

things to remember here the name of controller class will be use later on in your API URL so select the name properly and the controller class name should followed by controller.

In my case I have created controller with following name =”FriendController.cs”. So now your project structure is as shown below

CurrentProject_Structure

Step 4:- Now remember all the coding part need to be done here . I am creating two method here one method with name “GetAllFriends” and “GetFriendById”.

So as name suggest one will return list of all friends and one will return particular friend for a given id. If you see below instead of fetching data from database I have created a array of friends as shown.

ControllerDefinition

Now in above fig you saw two method with different return type. Now concentrate on GetFriendByID It’s return type is HttpActionResult. so if you see below in that particular method the return type is OK which means status code 200. It means http response will be return in this method.

Now another interesting part is you can add different attribute with method like HTTPGET, HTTPPOST,HTTPDELETE etc Just see in below screen shot as shown below.

HttpGetOrOtherAttributes

Now another interesting point is what if you want two attribute on same method then you can use below option which shown “AcceptVerbs”

AcceptVerbs

Step 5:- Now our most part is done now you need to access web API but before accessing web API you need to check route and if something is missing their then you need to modify WebAPI config  which you will get in App_Start function. In my case I have added API/{controller}/{action}/{id} which was not exist earlier. as shown in below fig

WebAPIConfigSettings

Step 6:- Now we are good to go now just run the application and type in url http://localhost:yourport/API/Friend you will get list of all the friends as shown below

FindListOfAllFriends

Now if you want to get specific friend with particular id then you have to write url like http://localhost:youport/API/Friend/1

You will get particular friend with that id as shown below

FindingSpecificFriendByID

 

Isn’t it simple ?

I hope you understand how easy to create Web API. Now in next step we will learn how to consume the Web API.

Mean while you can create different web api.

So enjoy till than

Your host,

Rajat

How to disable Right click on web page ?

Dear All,
Sometime, due to some security reference your application require to stop right click on web page.
In such case you need to write just simple code as shown below.

//
Right click disable

So now when you run your web page and right click you will get following message.

result
So just each code and you are good to go.

Hope you like it.

Thanks & Best Regards,
Rajat Jaiswal

How to create dynamic form using Javascript and Submit it in ASP.Net ?

 

Dear friends ,

sometimes you need to create dynamic form in asp.net . Let see below example to understand it more clearly.

Suppose on a  button click you want pass token value to a different  handler page then you need to write following lines

<script type="text/javascript">

function callPagePostBack(accessToken) {

var form = document.createElement("form");
            form.setAttribute("method", ‘post’);
            form.setAttribute("action", ‘LoginAuthentication.ashx’);

            var field = document.createElement("input");
            field.setAttribute("type", "hidden");
            field.setAttribute("name", ‘accessToken’);
            field.setAttribute("value", accessToken);
            form.appendChild(field);

            document.body.appendChild(form);
            form.submit();

}

</script>

When you run above code you will find on LoginAuthentication.ashx folder you are getting accessToken there.

 

I hope you enjoyed this tutorial.

Thanks ,

Rajat Jaiswal

How to invoke a Java script after completion of server event of asp.net control ?

}

Dear All,

Sometime you fall in this situation where you need to invoke a java script just after your server event of asp.net control. Lets  understand with an example suppose you have a  asp.net server control button on your web page now after clicking this button page will post back and everything is added in database and then after you need to show  Java script alert like Record is Saved successfully.

so to achieve this task you need to do following things

1.) Write a Javascript function for showing custom alert message  just like below

<script type="text/javascript">
        function onSuccessful() {

              alert(‘Data Saved Successfully’);

}

function onError() {

alert (‘Data not saved successfully’);

}

2) In server side button event you need to do following thing

protected void btnFaceBookLogin_Click(object sender, EventArgs e)
     {
         try
         {
             if (CheckLogin())
             {

ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "RegisterSuccess", "onSuccessful();", true);

}

else {

ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Registerfail", "onError();", true);

}

} catch (Exception ex) {

error.log(ex);

}

}

So when you compile it and run it after button click’s server side event javascript will fire according to checklogin  status.

Here the important note is to register the script with unique name.

I hope this will help you also.

Enjoy cheers 🙂

Rajat Jaiswal

How to Convert current date time different time zone (EST/PST/CST) in ASP.NET (C#) ?

Dear All,
Sometimes your application demands to covert date time in particular time zone.
.NET provided TimeZoneInfo class which help us in converting time in particular time zone.
Steps to utilize this is as follow
1) Convert time in any time zone firstly use UTC time.
2) Use TimeZoneInfo class to convert time zone from UTL to particular time

Below are some examples to utilize this

1) Convert time zone to EST (Eastern Standard Time)
DateTime EstDateTime = DateTime.UtcNow;
EstDateTime= TimeZoneInfo.ConvertTimeFromUtc(EstDateTime, TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time"));

2) Convert time zone to PST (Pacific Standard time)
DateTime PSTDateTime = DateTime.UtcNow;
PSTDateTime= TimeZoneInfo.ConvertTimeFromUtc(PSTDateTime, TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"));

3) Convert time zone to Central Standard Time
DateTime CSTDateTime = DateTime.UtcNow;
CSTDateTime= TimeZoneInfo.ConvertTimeFromUtc(CSTDateTime, TimeZoneInfo.FindSystemTimeZoneById("Central Standard Time "));

4) Convert time zone to GMT Standard Time
DateTime GMTDateTime = DateTime.UtcNow;
CSTDateTime= TimeZoneInfo.ConvertTimeFromUtc(CSTDateTime, TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time"));

I hope this will helpful to you also.

Thanks & Best Regards,
Rajat Jaiswal

How to remove scroll bar from Fancybox when using Iframe?

Dear All,
Suppose you are using JQUERY  FancyBox  popup one of the best popup and showing iframe in it but you are facing issue of scrollbar. You want to avoid scrollbar from fancybox then you have to do following things and I hope it will resolve your problem too.
So here is the solution 
$(document).ready(function () {   
         $(".fancybox").fancybox({             
   'titleShow': false,               
'autoscale': false,               
'autoDimensions': false         
   });      
  });

And in CSS you have to do following things
.fancybox-type-iframe .fancybox-inner {

overflow:hidden !important ;
height:500px !important;
}

It resolved my problem I hope it will resolve your problem too.

Thanks & Best Regards,
Rajat Jaiswal