Thursday, July 2, 2015
Routing to ASHX
Here is the piece of code that I found in CodeProject.com. By adding this extention method, you will be able to route the request to ASHX:
namespace System.Web.Routing
{
public class HttpHandlerRoute : IRouteHandler
{
private String _virtualPath = null;
private IHttpHandler _handler = null;
public HttpHandlerRoute(String virtualPath)
{
_virtualPath = virtualPath;
}
public HttpHandlerRoute(IHttpHandler handler)
{
_handler = handler;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
IHttpHandler result;
if (_handler == null)
{
result = (IHttpHandler)System.Web.Compilation.BuildManager.CreateInstanceFromVirtualPath(_virtualPath, typeof(IHttpHandler));
}
else
{
result = _handler;
}
return result;
}
}
public static class RoutingExtensions
{
public static void MapHttpHandlerRoute(this RouteCollection routes, string routeName, string routeUrl, string physicalFile, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null)
{
var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute(physicalFile));
RouteTable.Routes.Add(routeName, route);
}
public static void MapHttpHandlerRoute(this RouteCollection routes, string routeName, string routeUrl, IHttpHandler handler, RouteValueDictionary defaults = null, RouteValueDictionary constraints = null)
{
var route = new Route(routeUrl, defaults, constraints, new HttpHandlerRoute(handler));
RouteTable.Routes.Add(routeName, route);
}
}
}
To access the routing data in ASHX, you need to do this:
var o = context.Request.RequestContext.RouteData.Values["id"];
if (o != null)
{
q = o.ToString();
}
Reference:
http://www.codeproject.com/Tips/272258/ASP-net-HttpHandler-Routing-Support
Thursday, March 8, 2012
Extension method - BETWEEN function
public static bool IsBetween(this int value, int i, int i2) { return ((value >= i) && (value <= i2)); }Now, you may write this code for the integer data type:
int i = 5; if (i.IsBetween(1, 12)) { // the value is between 1 and 12 } else { // the value is out of range. }
By using extension method, you may create IsBetween() method for each data type and also your own data classes.
Saturday, July 16, 2011
Extension method (3)
You may use extension method to increase the code readability too. For example, we often ensure that the object instance return by some function is not null before using.
CustomerClass customer = GetCustomerObject(); if (customer == null) { // do this.. } else { // do that.. }
Wtih the extension method feature in C#, you may declare a new method call IsEmpty() which looks like this:
public static bool IsEmpty(this object obj) { return (obj == null); }
Now, you may do with your new IsEmpty() method:
CustomerClass customer = GetCustomerObject(); if (customer.IsEmpty()) { // do this.. } else { // do that.. }
Aside from improving the code readability, why we have to declare such IsEmpty() method instead of using "x == null"? This is what you are going to gain if you are using VS2010. When you type "." after the variable and follow by "Is", VS2010 will shortlist the methods that starts with "Is". Press Tab key to auto-complete the method name and you may move on to other code. This definitely will shorten the development time.
Note: you may rename this function to IsNull() or whatsoever.
Friday, June 24, 2011
Extension method (2)
Try this out:
string s = null;
int i = s.StrToInt();
Thursday, April 14, 2011
Extension method
This is a new feature that has been added to C# in C# 3.0. One of the usefulness of extension method is to shorten the codes that we used to write.
For example, we want to convert a string value to integer. Normally, we will write code like this:
int i = 0; if (Int32.TryParse(s, out i)) { return i; } else { return 0; }
Instead of doing this, we may already have declared a static function which will do the conversion like this:
public class MyClass { public static int StrToInt(string s) { int i = 0; if (Int32.TryParse(s, out i)) { return i; } else { return 0; } } }
So, we will write our code like this:
int i = MyClass.StrToInt("1234");
Now, with extension method, the data type conversion call will be shorten:
public static class MyClass { public static int StrToInt(this string s) { int i = 0; if (Int32.TryParse(s, out i)) { return i; } else { return 0; } } } public class Test11 { public void MyTestMethod() { // We don't have to write like this anymore: //int i = MyClass.StrToInt("1234"); // Now, we can do this: int i = "1234".StrToInt(); } }