ASP.NET MVC twitter/myspace URL style routing

February 15, 2010

(1) Comment

Disclaimer : This tutorial is meant for beginners and for anyone who find it useful. The main purpose of this post to keep everything i find useful for future reference for myself and for others.


With ASP.Net MVC url routing feature its also desirable to have twitter/myspace url style routing for your application. Its also give simple and shorter personalize url for your user.

There is 2 steps to accomplish this, first is to define the url route in routing tables and second is to define route constrains.

Step 1 : Add following route map in Global.asax below your default routes to handle personalize user url


// to handle personalize user url
routes.MapRoute
(
"user",
"{url}",
new { controller = "Link", action = "url", url = "" }
);

In the above code I let my url method in Link controller handle the request , basically the method will check the supplied user is exist or not and do whatever necessary .

Now you need edit your default route because when you try invoke user url like http://yoursite.com/user it will processed by the default url and you will get  The resource cannot be found error. So we need add url constraints to our default url so that it only processed the non user url. To do that we need create Custom Route Constraints , see the following blog post by Guy Burstein. I get the following code there , create the following class in your project..


using System.Web.Routing;
using System.Web;
public class FromValuesListConstraint : IRouteConstraint
{
private string[] _values;

public FromValuesListConstraint(params string[] values)
{

this._values = values;

}

public bool Match(HttpContextBase httpContext,Route route,string parameterName,
RouteValueDictionary values,RouteDirection routeDirection)
{
// Get the value called "parameterName" from the
// RouteValueDictionary called "value"

string value = values[parameterName].ToString();

// Return true is the list of allowed values contains
// this value.

for (int i = 0; i < _values.Length; i++)
if (_values[i].Equals(value))
return true;

return false;
}

}

Now we are ready to modify our default route , change your default route to something similar to following code


routes.MapRoute
(
"Default",                                              // Route name
"{controller}/{action}/{id}",                           // URL with parameters
new { controller = "Home", action = "Index", id = "" },  // Parameter defaults
new { controller = new FromValuesListConstraint("Home", "Account","Link") }
);

The parameters values is to tell the route engine which url should be handle by this route definition. Now your personalize user url should work accordingly. If you the url does’t work get ASP.NET Routing Debugger from Phil Haacked and test your urls.

BALA SINGAM

, , , , ,


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Online Java Compiler

October 7, 2009

(0) Comments

If you quickly compile a simple java program and you do not have the compiler in your machine there is online java compiler to compile your simple java program.

Online Java Compiler

BALA SINGAM

, ,


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Get list of view that uses certain column name

July 19, 2009

(0) Comments

Disclaimer : This tutorial is meant for beginners and for anyone who find it useful. The main purpose of this post to keep everything i find useful for future reference for myself and for others.

I was looking a query that tell me a list of view using certain view from a table, and found the answer here.

See the code below. Hope this could help someone somewhere.

select view_name from INFORMATION_SCHEMA.VIEW_COLUMN_USAGE
 where column_name='ColumnNameHere'
 and table_name='TableNameHere'

BALA SINGAM

, , ,


1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Loading ... Loading ...

How to shrink db log file

July 18, 2009

(0) Comments

I got this code snippet from my collegue , hope this can be helpful for others too

declare @dbname nvarchar(255)
set @dbname = 'databasename'
backup log @dbname with truncate_only
DBCC SHRINKDATABASE (@dbname, 0)

BALA SINGAM

, ,


1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.50 out of 5)
Loading ... Loading ...

Update top N record in SQL Server

June 9, 2009

(4) Comments

Disclaimer : This tutorial is meant for beginners and for anyone who find it useful. The main purpose of this post to keep everything i find useful for future reference for myself and for others.

SQL statement to update top N records

update daily_sales
set is_calc='Y'
from daily_sales
inner join (select top 100 * from daily_sales where product_category='CA') a
on a.unique_record_id=daily_sales.unique_record_id
and a.milestone_code=daily_sales.milestone_code

Source is SQLServerCentral

BALA SINGAM

, ,


1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...