Wednesday, January 15, 2014

MVC 4 URL Routing Engine

Before MVC framework coming in to action there was  a direct relationship between URL and files on the server.
http://CJUdawatteSite.com/default.aspx       d:/webroot/default.aspx

with MVC4 there was no 1-1 relationship for URL coming and file in the server.it associate with controller class and action method.

routes.MapRoute("YourRoute", "contoller}/{action}", new {controller ="Home", action ="Index", id= UrlParameter.Optional});

Ex : http://mydomain.com/Home/Index 
      http://mydomain.com

routes.MapRoute("", "Public/{controller}/{action}",new { controller = "Home", action = "Index" });

Ex : http://mydomain.com/Public/Home/Index 
      http://mydomain.com/Public

routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
routes.MapRoute("ShopSchema2", "Shop/OldAction",new { controller = "Home", action = "Index" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index",id = "DefaultId" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index",id = UrlParameter.Optional });   
routes.MapRoute("MyRoute", "{controller}/{action}/{id}",new { controller = "Home", action = "Index", id = "DefaultId" });
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional });
  
Prioritizing Controllers by Namespaces

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional},

new[] { "URLsAndRoutes.AdditionalControllers" });

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",id = UrlParameter.Optional},

new[] { "URLsAndRoutes.AdditionalControllers", "UrlsAndRoutes.Controllers" });



Route myRoute = routes.MapRoute("AddContollerRoute","Home/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index",
id = UrlParameter.Optional },
new[] { "URLsAndRoutes.AdditionalControllers" });

myRoute.DataTokens["UseNamespaceFallback"] = false;  //To disable searching for controllers in other namespaces


Constraining Routes

Constraining a Route Using a Regular Expression


In this example, we have used a constraint with a regular expression that matches URLs only where
the value of the controller variable begins with the letter H.

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*"},
new[] { "URLsAndRoutes.Controllers"});  

URLs only when the controller variable begins with the letter H and the action variable is Index or About.

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "^Index$|^About$"},
new[] { "URLsAndRoutes.Controllers"});

Constraining a Route Using HTTP Methods

The format for specifying an HTTP method constraint is slightly odd. It does not matter what name
we give the property, as long as we assign it an instance of the HttpMethodConstraint class. In the listing,
we called our constraint property httpMethod to help distinguish it from the value-based constraints we
defined previously.
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "Index|About",
httpMethod = new HttpMethodConstraint("GET") },
new[] { "URLsAndRoutes.Controllers" });  // Ex : httpMethod = new HttpMethodConstraint("GET", "POST") 











Tuesday, January 14, 2014

C# method extension

when you don't have code with you to edit certain library and want you to and some functionality.

I have Assembly called log and it has a class LogAction it has following functions
     

  1.        public  void WriteLog (LogType logtype)      
  2.        public  void WriteLog(LogType logtype, string Message)

let's assume we want to add third function called 
  • WriteLogWithException(LogType logtype,  Exception exc)
Add reference  to your project



then create static class like below


using System;
using LogLibrary; // here i call my library
namespace MethodExtension
{
    public static class Extension
    {
        public static void WriteLogWithException(this LogAction logaction, LogType logtype,  Exception exc)
        {
            logaction.WriteLog(logtype,exc.Message);
           
        }
    }
}
here we want two things to memorize extension class should be static and the method  you wish to write should be also static. 
You must specify the this keyword before the appropriate parameter you want the method to be called upon.

thank you.. :)