Monday, February 27, 2012

Get action name in view


If you want the name of the controller, action or the route values in your ASP.NET MVC view, this is how.
ViewContext.Controller.ValueProvider["action"].RawValue
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["id"].RawValue
Source: http://stackoverflow.com/questions/362514/how-can-i-return-the-current-action-in-an-asp-net-mvc-view

NDepend

Had a quick look at NDepend today, for a team that doesnt have higher editions of Visual Studio 2010.

For a tenth of the cost, it does have some useful features for code metrics and finding dependencies.

Given a choice, though, I would rather work with Visual Studio. Probably Resharper might be a better add-in if the intention is to have better code quality.

Saturday, February 25, 2012

URL Typos & Dubious Redirects

Have you heard of phishing attacks? Typically, they are done via spam mails, where the sender wants the recipient to think that the email is authentic, and so are the links within the email. A usual practice followed is to show a hyperlink that supposedly takes the recipient to a known/safe website, but the hyperlink actually points to a similar looking website that would capture the user credentials and other sensitive data. Beware of such links in the email. Use your browser instead. How exactly? Just a minute, I am coming to that.

Another form of phishing is where the user, in an attempt to get to a website, is lured to such a similar looking website, thanks to the typo of the URL (website address) in the browser. For instance, if I mis-type the URL for a website, such as wikipidea.org, instead of the original wikipedia.org, I get redirected to a dubious looking site such as channel-reward-central.com. This one, though, has a look similar to YouTube! But anyway, you get the idea. So, how do we overcome such issues?

For one, use a very little used feature of your browser's - the favorites links. In fact, browsers these days show you a list of frequently visited web sites when you open a new tab. So, that's a good place as well.

But what if you have got a new PC, have installed a new browser, or have just not visited your bank's website in a month, and it isnt saved in your favorites either? Or let's say, what if I want to install Firefox, and i am not sure whether I need to go to firefox.com or mozilla.com? These are situations when your favorite search engine can help as well. Hopefully, you have set that as your home page, or are able to type in the URL with errors, otherwise we are back to square one: goog.com or googe.com for instance is where you shouldn't go. Now, with google.com open, search for the website or the product you are looking for, and the first non-ad link should get you there safely.

Here are some more tips: http://support.google.com/websearch/bin/answer.py?hl=en&answer=8091

Apologies to those who feel this to be a typical case of too many words and too little substance. :)

URL and URI

The terms URL (uniform resource locator) and URI (uniform resource identifier) are often used interchangeably, and very often, this is not correct.

More to read here: http://en.wikipedia.org/wiki/Uniform_Resource_Identifier

To me, a URL points to a physical path to get to the resource, such as one that points to a file or a web page.

On the other hand, a URI is a logical identifier, such as a namespace in a schema definition.

Thursday, February 9, 2012

Base 64 Encoding

When?
If you need to store binary data in a string/text, you could use Base 64 encoding.

What is the problem with storing bytes as such?
Every byte contains 2^8 (256) possible values. But some of these values cannot be represented as text.

Why is it called base 64?
Now, if we think of the usual characters that can be represented as text, they would be A-Z, a-z and 0-9. That gives us 26+26+10 = 62 characters. Let's take 2 more like + and - (or /) and make this 64. Why 64 instead of 62? Since 64 is a round figure. Err, not in the decimal (base 10) system. But, in the binary (base 2) system.
2^6 = 64
So, now in this base 64 system, each of these 64 characters can be represented in 6-bits.

Note: If 8-bits can be called octets, let us call these 6-bits as hexets for now. I just made that up.
Googling for it, I find here that hexet is also, in German, a second-person plural subjunctive I of hexen, which has to deal with magic!

How can I convert an octet sequence into a hexet sequence?
Consider a byte sequence such as "Man". In terms of bytes, the values are 77, 97 and 110.
In terms of bits, these can be represent as 01001101 01100001 01101110
Now, if progress thru this sequence in hexets, we get
010011 010110 000101 101110
which is 19, 22, 5 and 46
which translates to TWFu
considering A-Z as 0-25, a-z as 26-51, and so on.

What if my byte sequence is not divisable by 3?
We pad zeroes at the end of the sequence to make it so. Also, we could introduce ending characters such as = to make up for missing characters.

Any issues with it?
Well, since each 3 octets translate into 4 hexets, it does increase the size of your byte sequence by 33%.

Is this being used anywhere?
The ASP.NET ViewState is one such place that comes to mind.

Can you tell me more?
It's all here. http://en.wikipedia.org/wiki/Base64

Tuesday, February 7, 2012

System objects

If we want to find text in a stored proc definition, we can use this
select * from sysobjects where id in (
select id from syscomments where text like '%TextToFind%')

This approach seems to be outdated though. These system tables were available in SQL Server 2000, and are available in SQL Server 2005/8 as compatibility views for backward compatibility.

The recommend approach is to use the new system views such as sys.sql_modules and sys.procedures. The above query can now be written as
select * from sys.procedures
where object_definition(object_id) like '%TextToFind%'

Reference:
http://databases.aspfaq.com/database/how-do-i-find-a-stored-procedure-containing-text.html