Posts

Understanding MVC Razor layouts

Image
Q) What are ASP.NET MVC3 Razor Layouts? You want a disclaimer, header and a menu on left to appear on all pages to bring in consistency to your web application. If you're from webforms background, you'd be quick enough to think of using a Master Page. Similar to Masterpage, MVC 3 introduces concepts of layout . Similar to selecting master page in ASPX pages in page directive, in MVC you can specify layout at global or specific view level. Q) When and how are they created? When you add a new MVC3 application (either blank or internet or intranet), Visual Studio automatically adds a default layout file called “ _layout.cshtml ” , placed in shared folder of view. Note: There is no special extension for layout file. This layout is  automatically wired in another auto generated file called “ _viewStart.cshtml ” which contains following code: @{     Layout = "~/Views/Shared/CustomLayout.cshtml"; } Of course, you can replace CustomLayout.cshtml with your custom ht...

Handling CSS with JQuery

1. Modify class at runtime 1.1. If an element has multiple classes attached and you need to remove one of class (say 'oldclass') $(selector).removeClass("oldclass") 1.2. If you want to add a new class to an element (say 'newclass') $(selector).addClass('newclass') 1.3. If you want to replace one of the class associated with element (say replace 'oldclass' with 'newclass') $(selector).removeClass('oldclass').addclass('newclass') 1.4. If you want to replace all classes with a different class $(selector).attr('class','newclass') // with this one 2. Notes on using multiple classes. 2.1. If you want to specify multiple classes to an element, separate them with space. <div class="class1 class2 class3"></div> OR $('div').addClass('classA classB') OR $('div').addClass('classA').addClass('classB') 2.2. More than the sequence in which classes are spe...

Throw your own - Custom Exceptions

The key to understanding custom exception concept lies in knowing how to catch them. You have to know syntax, and you have to know how to use it. First, the syntax itself try {} catch (CustomException $e) {} catch (OtherException $e) {} catch (Exception $e) {} Basicaly it looks like an switch/case statement. The above code could be written as the next one, and all will be valid and will work perfectly. The try catch syntax is actually automatically handled multiple if statement! try {} catch (Exception $e) { if ($e instanceof CustomException) {} else if ($e instanceof OtherException) {} else {} } Throw your own Note that by distinguishing exceptions you can decide which are the fatal errors and which exceptions are only passing you an information that something is wrong and it is a handled case. This means that you have to use Exceptions not only on critical situations. Throw them for your handled situations too. class MyAuthException extends Exception {} class MyTest { pri...

Throw your own - Losing the fear of Exceptions

The best way to lose the fear of exceptions is to start throwing them. Recipe is simple: whenever you come to case that something is wrong and your function or method can not accomplish its task, just throw an exception. Example 1 My favorite case is when I'm using a configuration values in my applications. Ops, there is no value at all, not an numeric value? I'll just throw an exception and stop thinking about that. This case is especially important if your application will have several installations. This way you are ensuring that nobody (including you) can not forget to fill in configuration. Or, when such case happen anyway, problem will be immediately identified. class MyIni { private $_config; public function __construct( $config) { $this->_config = $config; } public function getProductId() { if (!isset($this->_config['productId'])) throw new Exception( 'Required config parameter [productId] is not set'); if (!is_numeric( $this...

Starting a Thread targeting method with parameters

Lets say you want to start a thread. How would you do it? Thread  T1 =  new   Thread ( new   ParameterizedThreadStart (SomeMethodName)); Restriction imposed by .NET is that Method which T1 targets can have only one parameter of object type. Hence, above code will work only if method signature is: public   static   void  SomeMethodName( object  o) But what if I want to invoke a method like: public   static   void  xyz ( int  a,  int  b) ??? Answer is to use delegates here and start thread as: Thread  T3 =  new   Thread ( delegate () { xyz(3, 4); }); or Thread  T3 =  new   Thread (() => xyz(3, 4));

The following module was built either with optimizations enabled or without debug information Visual Studio

Image
If you are facing this issue for long you must be on verge of banging your head or throw away your PC. There are many cause for this error. You might have tried options and may have missed a few. Following is a summary of different things that have helped me in resolving this issue (Hope it helps you out too): 1. In your Visual Studio, Go to Tools>Options>Debugging>General and check "Enable just my code as shown below" 2. If your application is configured to run using a IIS URL instead of dynamic localhost port, just ensure that IIS directory is pointing to your directory. Often while testing we make different copies of application and keep pointing application in IIS to one or the other. This actually resolved my issue. 3. Clean your solution, close VS, open again, rebuild application and try 4.Open properties of your project and ensure configuration as below: 5. Continuing from above, press advance button in Build settings below which opens a popup. Ensure, Debug I...

Delete network credential from IE

If you have mistakenly opted for "remember password" while opening sharepoint site (or any other site which opens a popup up for network credentials), then forcing IE to again start prompting for credentials is a pain unless you know the right think to do. Everywhere you will get replies to delete cookies and other files by going into IE8>Settings>Content>AutoComplete>Delete Files. But this doesn't works for network credentials as they actually get stored by windows. Instead do following: STEPS (applies to windows 7) 1. Click on start button 2. Type passwords and select Manage network passwords option from filtered list.      You will find saved URLs and passwords of saved credentials 3. Select Delete option for the saved credential you want to remove