pages

Tuesday, 24 November 2015

HTTP verbs

Many developers still don't know what is Http verb and how many of them are there and what is its actual use how web server treats them differently.

For Http 1.1

  • Get - Retrieves the information identified by URI
  • Head - Retrieves the message headers for information identified by URI.
  • Put - Stores the enclosed entity under the request URI.
  • Post - Post a new entity enclosed in the request.
  • Delete - Deletes the resource identified by URI.

Get and head are considered to be as safe methods becuase they are just meant for retrieval.

More detailed info can be found on W3C website

Monday, 23 November 2015

Getting started with Node

Javascript based frameworks are now a new buzzwords in development be it Angular or Node. The best thing with node is that you can write server side code also in javascript. Basically you can run your javascript applications outside the browser.

Node works on v8 Javascript engine developed by Google. Now you can work on one language throughout your application. No need to say and I quote "I am using blah-blah for my serverside code".  :)


You can download Node from Nodejs.org.

Node comes with a package manager called NPM(Node Package Manager). If you are familiar with Nuget or choclatey it won't be difficult for you to get started with this.


Using Visual Studio to code Node is a good idea you can download a free version of visual studio from Visual Studio website here and can install Visual Studio tools for Node. This page has a wiki link(Documentation) and also contains guidelines and other tips.

Start learning node by coding. There many tutorials available at Node School. Go through API here.


Happy Coding !!

Shortcuts in SQL server

Knowing shortcuts is very handy for a developer you dont have to touch mouse and break the rhythm. I came across one interesting shortcut in SQL Server.
alt+f1

For any SQL object in your database you can use this shortcut in Management Studio

See the sceenshot below. Just selected the table name and pressed the shortcut will give you all the details




Lots of other shortcuts are available here.. Management Studio Keyboard Shortcuts

Sunday, 22 November 2015

Writing a simple web crawler

Crawling is a thing which all search engines do across the web. This is a simple web crawler which crawls the the page you give and will give you back all the links on that page. Here for the sake of example I took Google.com.



using System;
using System.Net;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;


public class Crawler
{
	public static void Main()
	{
	string url = "http://www.google.com";
	HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
        httpWebRequest.UserAgent = "Anurag's Crawler";
        WebResponse webResponse = httpWebRequest.GetResponse();
        Stream stream = webResponse.GetResponseStream();
        StreamReader streamReader = new StreamReader(stream);
        string htmlText = streamReader.ReadToEnd();
	var allLinks = GetNewLinks(htmlText);
		foreach (var link in allLinks)
		{
			Console.WriteLine(link);
		}		
	}
	
	
	
	private static List GetNewLinks(string content)
{
    Regex regexForLink = new Regex("(?<=<a\\s*?href=(?:'|\"))[^'\"]*?(?=(?:'|\"))");
    List<string> newLinks = new List<string>(); 
    foreach (var match in regexLink.Matches(content))
    {
        if (!newLinks.Contains(match.ToString()))
            newLinks.Add(match.ToString());
    }

    return newLinks;
}
}


Web Crawler  This is a crawler written using Reactive Extension
One more web crawler thats available and bit complex Archnode.net

Tuesday, 24 February 2015

Passing Exception to Controller from Global asax

Came across this scenario where we have to redirect for all un-handled exceptions to user diplaying the StackTrace, Exception message and other details.

Global.asax
protected void Application_Error(object sender, EventArgs e)
        {
            Exception exception = Server.GetLastError();
            Response.Clear();

            RouteData routeData = new RouteData();

            // Routing to ~/Error/Error
            routeData.Values.Add("controller", "Error");
            routeData.Values.Add("action", "Error");
            routeData.Values.Add("error", exception);

            // clear error on server, not to display yellow error screen
            Server.ClearError();

            IController home = new TWC.IssueTrak.Mobile.Controllers.HomeController();
            home.Execute(new RequestContext(new HttpContextWrapper(Context), routeData));
            
        }


ErrorController
 public ActionResult Error(Exception exception)
        {
            return View("~/Views/Shared/_Error.cshtml",Exception);
        }


Error.cshtml
@model System.Exception
<div>Exception Message: <b>@Model.Message</b></div>

Thursday, 8 May 2014

Ajax Control Toolkit : Popup Extender in Update Panel

I was working with Ajax Control toolkit controls. Came across a bug which was "Popup not closing after data loads.". Looked into the details of the bug found out particularity of scenario. The popup was shown using Popup Extender of Ajax control toolkit, Popup is inside the Update Panel.
On button click (which is in Popup) partial page load is happening. Once getting data from server, popup was supposed to get close, But was not behaving in expected manner.
I digged a little bit here and there to fond out that after ajax call Modal popup is not calling the hide method.
So we have to call the hide method on Modal Popup Extender.

I tried calling hide() method directly in script block but it was returning "null". The reason being i was trying to find the component before component initalization. So for calling hide method after component initialization i had to call it in Sys.Application.add_load.
 <act:ModalPopupExtender ID="mpe" runat="server" TargetControlID="LinkButton1" PopupControlID="Test" BehaviorID="mpeBehavior"


<script type="text/javascript">

    Sys.Application.add_load(function () {
$find("mpeBehavior").hide();
  });
</script>

Thursday, 30 January 2014

Generating change scripts in SQL Server

I have seen many times that developers do modification in database using VisualDesigner and struggle later on with providing scripts to other people (in case of local server). Modifying the Table structure and similar things are tasks that should be taken seriously and it might be hard to write the code remembering all the DDL statements. Specially when we have such a nice Designer provided by Microsoft who wants to get dirty by writing the code.

How to Generate Change Script:
In SSMS(Sql Server Management Studio) goto Tools -> Options.



In popup that got opened goto Designers -> Auto Generate Change Scripts.



Select the checkbox against it and you have sucessfully configured your sql server to generate the change script automatically.

NOTE: Sql Server will not generate any change script for Drop Table. ;-)