ASP.NET MVC framework uses the ASP.NET routing engine

URL Routing

Agenda

  • Overview
  • Mapping URL to Controller Action Method
  • Applying Constraints on Route parameters
  • Resolving Namespace Ambiguity
  • Ignoring Routes

URL Routing Overview

The ASP.NET MVC framework uses the ASP.NET routing engine, which provides flexibility for mapping URLs to controller classes. You can define routing rules that the ASP.NET MVC framework uses in order to evaluate incoming URLs and to select the appropriate controller. You can also have the routing engine automatically parse variables that are defined in the URL, and have the ASP.NET MVC framework pass the values to the controller action methods as parameters.

Global URL Routing Defaults:

Routes are initialized in the Application_Start method of the Global.asax file. The following example shows a typical Global.asax file that class RourteConfig.RegisterRoutes which includes default routing logic.

public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// . . .
RouteConfig.RegisterRoutes(RouteTable.Routes);
// . . .
}
}
public class 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 }
);
}
}

Implementing Routing for Employee Application:

Add following route methods to the Global.asax file

routes.MapRoute(
"Create", // Route name
"Create", // URL with parameters
new { controller = "Employee", action = "Create" } // Parameter defaults
);
routes.MapRoute(
"Edit", // Route name
"Edit/{Id}", // URL with parameters
new { controller = "Employee", action = "Edit" } // Parameter defaults
);
routes.MapRoute(
"Delete", // Route name
"Delete/{id}", // URL with parameters
new { controller = "Employee", action = "Delete" } // Parameter defaults
);

Note:All the entries must be above “Default” route because otherwise it will take precedence and others will not be useful.

Applying Constraints on Route parameters

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{name}/{id}",
defaults: new { controller = "Home", action = "Index", name="test", id = UrlParameter.Optional },
constraints: new { name="[a-z]{5}", id = @"\d{1,8}" }
);

The above route puts a constraint on ID that it should be between 1 and 8 digits only.

Example 2: Overloaded version of MapRoute

routes.MapRoute("Default ",
"{controller}/{action}/{id}",
new RouteValueDictionary { //Default Values
controller = "Home",
action = "Index",
name="test",
id = UrlParameter.Optional },
new RouteValueDictionary { //Contraints
{ "name", "[a-z]{5}" },
{ "id", @"\d{4}" } });

 Resolving Namespace Ambiguity

If we have two HomeController Classes in different namespaces.

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "MyDemoApp.Controllers" }
);
//OR
ControllerBuilder.Current.DefaultNamespaces.Add("MyDemoApp.Controllers");

Ignoring Route URL’s

If routes.IgnoreRoute(“Home/Index/123”); is added to RegisterRoutes method

the url: http://localhost:49165/Home/Index/123

will render HTTP Error 404.0 – Not Found

soap request and response example in asp.net

  • Every SOAP request and response are made up of SOAPEnvelope. SOAPEnvelope has SOAPHeader, SOAPBody, SOAPFault.
  • SOAP always uses Http Post method and the SOAPEnvelope is submitted as MessageBody.
  • o A Webservice developed in .Net can be used by an Http Client also. Here the method name is submitted as the PathInfo and the parameters are posted as a string of Name-Value pairs.

http:// ServerName/ApplicationName/ DemoService.asmx/ MethodName

SOAP Request

POST /DecWebServices/DemoService.asmx HTTP/1.1

Host: localhost

Content-Type: text/xml; charset=utf-8

Content-Length: length

SOAPAction: “http://tempuri.org/Add”

<?xml version=”1.0″ encoding=”utf-8″?>

<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>

<soap:Body>

<Add xmlns=”http://tempuri.org/”>

<a>int</a>

<b>int</b>

</Add>

</soap:Body>

</soap:Envelope>

SOAP Response

HTTP/1.1 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: length

<?xml version=”1.0″ encoding=”utf-8″?>

<soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/”>

<soap:Body>

<AddResponse xmlns=”http://tempuri.org/”>

<AddResult>int</AddResult>

</AddResponse>

</soap:Body>

</soap:Envelope>

Note: To view the WSDL document we can use the following URL
http://localhost/SepWebServices/Service.asmx?wsdl

Client Application: (In New Instance of VS.NET)

Step 3: File –> New Project –> Windows Application
Step 4: In the Solution Explorer –> References –> Right Click –> Add Service Reference –> In URL give the Url of Web Service(can copy from browser where we can see the webservice methods) –> Click on Go.
Step 5: In the same dialog: Namespace = localhost (name of domain on which the webservice is hosted is used) –> Click on Add Reference
Note: When a WebService reference is added in VS.Net that is using add Web reference it automatically creates a proxy class in the current project using a utility program called asWSDL.EXE. The same can be viewed by opening the file Reference.cs. (Before this click on Show All Files in Solution Explorer).
Step 6:
using WebServiceClientAppName.localhost
private void btnDemo_Click(object sender, EventArgs e)
{
DemoServiceSoapClient ds = new DemoServiceSoapClient();
MessageBox.Show(ds.Add(20, 50).ToString());
}

Step 7: Run the Client Application
Note: If a new method is added to the webservice class, right click on web reference of the webservice and select Update ervice Reference to reflect the new method in the proxy class.

distributed computing framework in .net

Distributed Computing Architecture

  1. Proxy is a client side representative of the actual object on server. It implements the same set of interfaces as the actual object implements.
  2. When the client requests for an instance of a remote object, the underlying services (Framework) creates the actual object in the server process and the proxy object in the client process, the client gets the reference to the local proxy object.
  3. Along with the actual object a “stub” object is created on server. The proxy and stub are connected with each other over a socket and communicates with each other using a common protocol.
  4. Whenever the client calls the method of the proxy, the implementation in Proxy serializes all the parameters of the method and transports them to Stub i.e. proxy marshals the request to stub.
  5. On Server, Stub deserializes the request and passes the references of the objects as parameters to the actual method in actual object.
  6. Server method returns data to Stub, which serializes the data and return to proxy, proxy deserializes and return the data to the client object.

SUN (Stanford University Network – RMI (Remote Method Invocation) – JRMP(Java RMI Protocol) OMG (Object Management Group) – CORBA Common Object Request Broker Arch. -IIOP (Internet Interoperable Protocol MS (Microsoft) – DCOM (Distributed Component Object Model) – DEC – Object RPC(Distributed Computing Environment)

Problem: Because the proxy and the stub are tightly coupled to each other they must use the common protocol for communication. As the protocols used by RMI, COBRA and DCOM are native protocols they lack Interoperability.

WebService

A WebService is a business object running on a Web Server and providing its functionality to any type of application / client which supports “SOAP” (Simple Object Access Protocol) protocol.

SOAP = XML + HTTP. SOAP is a specification by W3C. – Its an architectural neutral protocol.

In .Net WebSevices are managed by ASP.NET Framework. They are mostly used for developing B-B (Business to Business) services/application. Any kind of application which we can develop in MS.NET can be a client to WebService.

WSDL (Web Service Description Language):

It is an XML based document containing complete information of the webservice including methods and their parameters, the URL etc…

For every webservice developed, the corresponding WSDL document is distributed to the clients. The clients using a native utility program generates a proxy class from a WSDL document. This proxy class then can be used by client application for invoking the functionality / methods of the webservice without worrying about the existence of SOAP protocol.

Steps for Developing WebService:

File–>New Web Site  ASP.NET Web Service and rename Service.asmx to DemoService.asmx

In DemoService.asmx:

  • A webservice class autogenerated by studio has the attribute [WebService] and is also inherited from System.Web.Services.WebService class. BOTH ARE OPTIONAL REQUIREMENTS. i.e. any public class linked through file with extension ASMX and deployed in the webapplication can treated as a WebService.
  • All the methods of a webservice class to be used by the clients on internet must be marked as “WebMethod” ( attribute)

Step 2 [WebMethod]
public int Add(int a, int b)
{
return a + b;
}