URL Routing in MVC

This post gives a quick insight of URL Routing in MVC.

Overview

  • A Route is a URL Pattern mapped to a handler. A Handler can be physical file or a class file, that does processing
  • URL routing is about freedom from URLs referring to physical files in a web application.
  • In an MVC URL Routing, Actions are mapped to respective Controllers.

Example :

Traditional URL mapping (Web Forms Application) : “localhost/Vehicle/Detail.aspx?VehicleID=12”

This narrates that our web application contains a directory named “Vehicle” & within this folder we have a physical file “Detail.aspx”

URL Routing in MVC  Application : “localhost/Vehicle/Detail/12”

This narrates that our MVC application has a controller “VehicleController” having an Action “Detail”

When we create an MVC 4 web-application it adds following piece of code in RouteConfig.


public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
 

How It works.

Our default routing contains  3  parameters

  1. {controller}  : Name of controller class
  2. {action} :  Name of Action within the controller class
  3. {id} : Parameter needed to process Action

For each incoming request , the MVC framework first locates the controller file, “HomeController”  & then locates the Action “Index” within the controller. Finally It passes parameter {id} to execute located Action.

It is important to note that we don’t need to mention full name of controller as it automatically appends “Controller”  to “Home” & starts locating “HomeController”

Reference :

http://msdn.microsoft.com/en-us/library/cc668201.ASPX

Advertisements

WCF vs WebService

This post quickly explains difference between WCF & WebServices.

1. Protocols:

  • Web service supports HTTP via SOAP.
  • WCF supports HTTP, TCP,MSMQ, UDP , Named Pipe and many custom formats  SOAP, JSON, REST etc

2. Security:

  • Web Service provides only transport level security.
  • WCF supports Transport Level , Message Level & Data Level Security.

3. Hosting:

  • Web Service can be hosted in IIS only
  • WCF  can be hosted on IIS, WAS- (Windows Activation Server) , Managed Windows Service. Also it can be self hosted ( windows form , console app )

4. Migration:

  • Any .net distributed applciation can be easily migrated to WCF with very fewer changes.
  • Not easily possible in Web Service.

5. Serialization:

  • In order to serialize a web service we need to explicitly specify serialization attributes using  XML Serialization
  • WCF incorporates  Serialization by default for exposed methods using WCFSerialization.

ActiveX Explained

This post provides a quick overview of ActiveX.

ActiveX are Extension of OLE.  Primarily defining how information would be shared by applications. ActiveX focus is on connected environment particularly World Wide Web. Microsoft added ActiveX support to Internet Explorer (IE 3.0) in 1996, It would download certain ActiveX for websites to enhances user experience. It has more control to windows operating system, thus it is more prone to associated security risks. ActiveX is only supported by Microsoft Operating Systems.

OLE Explained

This post provides a quick overview of OLE – (Object Linking & Embedding).

OLE – simply is a standard to embed different documents together. One document type containing an object of another document type is said to be a compound document. The contained /embedded document objects retain their properties. This

A common example of OLE is a spreadsheet object (MS Excel) being contained within a word processor (MS Word ). While the user would only be able to edit a document type ( MS Word / MS Excel) If the required software (that understands the document type) is installed on machine.

Dynamic Data Exchange (DDE) Explained

This is a quick overview of DDE – Dynamic Data Exchange.

Windows applications often require to exchange information with multiple application instances or even with different application instances. These applications may be running on a single machine or on several machines in a network environment. Several standard protocols have been developed to address these requirements. This communication is referred as interprocess communication.

DDE – Dynamic Data Exchange is among earlier versions of such data transfer protocols. It sends data to & from applications. This protocol can be used for one time data exchange or progressive data exchange. DDE was first introduced in 1987 & since it uses Windows Messaging Layer, therefore it still works in modern versions of Microsoft operating systems.

DDE – supports several Data Exchange formats & custom formats could be defined for specialized inter-process communications.

MVC4 – Mobile Application Template

This is my third post on Exploring MVC 4 Templates. & i will discuss“Mobile Application Template”

In Visual Studio 2010 . Create a new ASP.NET MVC4 Project.  Select “Mobile Application” from given list of Templates & click OK.

mvc_Mobile_Template

This should create a project for you with following defaults.

mvc4_Mobile_Defaults

We can see a two controller files added under Controllers Dir with a corresponding Views.

Also if we explore Scripts folder we can notice jquery Mobile library , that helps the content to dynamically fit for browsers on device.

mvc_Mobile_Scripts

Hit F5 & you should be able to see the application running.

We also need to see how will the page look like on actual device, Therefore we will launch Windows Phone 7 Emulator, & browse to our webpage url. (eg : http://localhost:62174/ )

You should be able to view the first page on your emulator.

MVC4 – Internet Application Template

This is my third post on Exploring MVC 4 Templates. & i will discuss“Internet Application Template”

In Visual Studio 2010 . Create a new ASP.NET MVC4 Project.  Select “Internet Application” from given list of Templates & click OK.

MVC4_InternetApplication

This should create a project for you with following defaults.

MVC4_InternetApplication_Defaults

We can see a HomeController.cs file added under Controllers with a corresponding Home View.

Hit F5 & you should be able to see the application running.

Extracting Compressed Files in C sharp

Recently I came across a requirement of extracting a compressed zip files AGAIN, & I recalled dealing with same requirement quite a long ago…. but couldn’t figure out what library I used then.

It (the chaos) strongly urged me to have an archive where I can place some of my findings as a Quick reference for myself and probably others too :) .

I used http://dotnetzip.codeplex.com/ as a reference. The Dll it uses is Ionic.Zip.

The code samples are very brief and provide a variety of ways to handle issues related with compressed files.

 
By Sarfraz Ahmed Khan Posted in .NET

MVC4 – Basic Project Template

This is my second post on Exploring MVC 4 Templates. & i will discuss“Basic Project Template”

In Visual Studio 2010 . Create a new ASP.NET MVC4 Project.  Select “Basic” from given list of Templates & click OK.

BasicProjectTemplate

This should create a project for you with following defaults.

mvcBasicProject

A Basic MVC project has no files under Controllers, Views & Models. Comparing with “Empty” Project,  App_Start folder contains BundleConfig.cs file & there is a Scripts folder in the solution. Project disk size is less than 30 MB.

In order to make this template working, we will add a HomeController & a Index view like we did in this post.

MVC4 – Empty Project Template

This is my first post on Exploring MVC 4 Templates. & i will start with “Empty Project Template”

In Visual Studio 2010 . Create a new ASP.NET MVC4 Project.  Select “Empty” from given list of Templates & click OK.

EmptyProjectTemplate

This should create a project for you with following defaults.

EmptyProjectDefaults

An Empty MVC project provides the minimum set of resources & references to run MVC4 application. Notice that we have no files under Controllers, Views & Models. Project disk size is less than 12 MB.

Hit F5 key to run your application.

Get surprised to see an error page , telling that “The resource cannot be found.” This is because the ASP.Net engine expects a Controller & a View Object.

To make your project work, add a controller to your project.

AddController

Notice that we have selected “Empty MVC Controller” Template.

Now we need to add a View Object in our solution.

AddView

We will keep our View file under Home folder

SlnExplorer

Now if we hit F5, we should be able to see a page without any errors.