Showing posts with label solutions. Show all posts
Showing posts with label solutions. Show all posts

Friday, October 15, 2010

How to stop automatic newline in ASP.NET code blocks

I want to keep my one liner code blocks in my ASP.NET MVC views as:

  1. <% Html.BeginForm(); %> 

Unfortunately, a not so helpful feature recognize the C# code and reformats to the following:

  1. <%  
  2.    Html.BeginForm(); %> 

Thanks to Erv Walter for solving the problem.

In short, to remove this feature we have to:

  • Go to Tools – Options – Text Editor – HTML – Formatting
  • Select Tag Specific Options…
  • Under Client HTML Tags, add three tags: “%”, “%:”, and “%=”

The %-tag should have line breaks before and after, and no closing tag.

The %: and %= tags should have no closing tag, and no line breaks.

For ReSharper-users, also remember to disable Auto-formatting on semicolon, and on closing brace. Otherwise, ReSharper will interfere and reformat.

Tuesday, September 21, 2010

Blank page in ASP.NET MVC, IIS and Windows 7

You decide to move your web application from the internal web server to IIS under Windows 7, but observe nothing but a blank page.

Seems like it’s not enough to just add IIS from within Programs And Features – Turn Windows features on or off, but some additional tricks are required.

When activating World Wide Web Services within Turn Windows features on or off, remember to include:

  • HTTP Errors
  • HTTP Redirection

Even if ASP.NET is already selected, the following command line must be executed. Remember to run cmd as Administrator:

  1. %windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis.exe –ir   

Resources:

Friday, September 17, 2010

Can’t connect MyMobiler due to a busy ActiveSync connection

I installed MyMobiler on my computer, but couldn’t connect to my HTC HD2. The exact words in the message has slipped my mind, but it said something about an ActiveSync connection being busy.

In my case, that message was both unhelpful and totally wrong. The real reason was missing drivers on my Windows 7 64-bit system:

  1. Download and install the drivers.
  2. Reconnect the phone, make sure to select the ActiveSync option.
  3. Wait for the driver to complete the install.
  4. Exit MyMobiler and start it again.

Resources:

Thursday, September 16, 2010

Online code syntax highlighter

I never get around to find out how to modify my blog template to include javascript for syntax highlighting.

So I use this online service: http://www.thecomplex.plus.com/highlighter.html

Use Mercurial to remove TFS friction

TFS, unfortunately, does some things wrong by default. If the client is anything except Visual Studio, the user experience is abysmal when it comes to commit file changes.

These days I created wireframes in Mockups for all my screens. But checking in those files were so difficult that I routinely procrastinated the task. I have installed TFS Power Tools with its shell extension, but that UI suffers from the same usability issues.

One possible workaround is to use TortoiseHg, which is pretty frictionless. Eric Hexter at LosTechies has an excellent recipe on how to set up Mercurial as a local repository for TFS. He didn’t, however, provide the source code for the Power Shell scripts. I typed them up here with minor adjustments.

Pull.ps1


  1. $projectName = "your-project"  
  2. $tf = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"  
  3.  
  4. function pull{  
  5.     cd "\your\tfs\folder"  
  6.     &$tf get  
  7.     hg commit -A -m "from tfs"  
  8.     cd "your\working\hg\folder"  
  9.     hg pull --rebase  
  10. }  
  11.  
  12. pull 

Push.ps1


  1. $projectName = "your-project"  
  2. $tfpt = "C:\Program Files (x86)\Microsoft Team Foundation Server 2010 Power Tools\tfpt.exe"  
  3. $tf = "C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\tf.exe"  
  4. hg push  
  5. cd "\your\tfs\folder"  
  6. &$tfpt scorch /noprompt /exclude:.hgignore`,*.ps1`,.hg  
  7. hg update -C -y  
  8. &$tfpt online /adds /exclude:.hgignore`,*.ps1`,.hg`,_ReSharper`,bin`,obj`,*.user`,*.suo  
  9. &$tf checkin  
  10. cd "your\working\hg\folder" 

Bulk unblock files in Windows 7

When you download files from unsafe sources like the internet, Windows will helpfully block those files to protect you.
I find that one of the most annoying and unusable feature in Windows 7.
Not because I don’t want to be safe, I do, but that feature gets in my way more often than not.
Usually, I download Visual Studio solutions with the usual libs folder, or binaries I trust. My download pattern is to let my browser open the zip file, extract all files from there, and start digging.
At this point I have all forgotten about the download, the location of the download folder, or worse, the temp folder. So when I now is prohibited by Windows to do my work, I get annoyed because there are no simple way to unblock all files in a directory tree.'
Here’s a way to make life easier:
  • Download  Streams.
  • Remember to unblock the zip file.
  • Unzip and copy streams.exe to \Windows\System32 or any other directory in your path environment variable.
Now we are ready for the manual option:
  • Hold down Shift and right-click the blocked folder.
  • Select “Open command window here”
  • Execute the following command “streams -s -d *.*”
For the automatic option, we need to modify the registry:
  • Create a new text file and rename it to something like “unblock-files-in-here.reg”.
  • Paste in the text below
  • Run the file.
  • Right-click a file and select “Unblock file”
  • Or right-click a folder and select “Unblock files in here”

Registry script:


  1. Windows Registry Editor Version 5.00  
  2.  
  3. [HKEY_CLASSES_ROOT\*\shell\unblockfile]  
  4. @="Unblock file"  
  5.  
  6. [HKEY_CLASSES_ROOT\*\shell\unblockfile\command]  
  7. @="cmd /c streams -d \"%1\""  
  8.  
  9. [HKEY_CLASSES_ROOT\Directory\shell\unblockfilesinhere]  
  10. @="Unblock files in here"  
  11.  
  12. [HKEY_CLASSES_ROOT\Directory\shell\unblockfilesinhere\command]  
  13. @="cmd /c streams.exe -d -s \"%1\""  

Resources:

Wednesday, September 09, 2009

The most important thing nobody told you about model binders

A bold statement, but I stand by it.

I have done numerous searches on how to implement custom model binders in ASP.NET MVC, and all of them are variations of:

public override object GetValue(ControllerContext ctx, string modelName, Type modelType, ModelStateDictionary state)
{
   Customer customer = new Customer();
   customer.FirstName = ctx.HttpContext.Request["FirstName"];
   customer.LastName = ctx.HttpContext.Request["LastName"];
   // ... other properties ...
   return customer;
}


But that’s counter productive, isn’t it? ASP.NET MVC is all about conventions and flexibility. The last thing I want from my model binders are to hardcode expected property names, which would fail anyway. If my type is ReportIdentificator, I certainly don’t want to give all my properties and parameters the very same name. I want my freedom to call them foo and Bar, if that’s what I need.



So I finally went to the source, so to speak. The ASP.NET MVC source code is available, and inside of the DefaultModelBinder, I found the following gem:



    if (!performedFallback) {
        ValueProviderResult vpResult;
        bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out vpResult);
        if (vpResult != null) {
            return BindSimpleModel(controllerContext, bindingContext, vpResult);
        }
    }


The getaway here is bindingContext.ModelName. That property is the key to get the proper ValueProviderResult, which contains the posted values for the model. With that information, I am finally able to create the model binder I want.



The usual binder registration:



    ModelBinders.Binders.Add(typeof (ReportIdentifier), new ReportIdentifierBinder());


And my binder:



    public class ReportIdentifierBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var model = (ReportSectionIdentifier)base.BindModel(controllerContext, bindingContext);
            ValueProviderResult serialized;
            if (model == null && bindingContext.ValueProvider.TryGetValue(bindingContext.ModelName, out serialized))
            {
                var values = serialized.AttemptedValue.Split("-".ToCharArray());
                model = new ReportSectionIdentifier
                            {
                                Organization = int.Parse(values[0]),
                                Period = int.Parse(values[1]),
                                Report = int.Parse(values[2]),
                            };
            }
            return model;
        }
    }


I let the base try to create the model first, in those cases where the usual conventions are followed. My implementation deserialize the model from a value on the form “1-2-3”. That code enables me to support drop down lists bound to a report name and its identifier, which posts a value on the form “1-2-3”, or use the identifier in action links, which appends the property values to the querystring according to the conventions.

Monday, July 27, 2009

The specified module could not be found (vscomptr.inl)

An explanation and solution is found here.

You do something in Visual Studio 2008 that causes a “Get Latest” or “Check Out”, and are greeted with the following message:

Unexpected error encountered. It is recommended that you restart the application as soon as possible.

Error: The specified module could not be found.

File: vsee\internal\inc\vscomptr.inl

Line number: 618

Reinstalling OleDB may fix it. Open command prompt, and:

  1. cd "c:\Program Files\Common Files\System\Ole DB"
  2. regsvr32 oledb32.dll

Tuesday, July 21, 2009

The project type is not supported in Visual Studio 2008

You download some source code demonstrating ASP.NET MVC, a library or an application built on this MVC framework, but when you open the .sln-file, you are greeted with the following error:

The project file ‘your-project.csproj’ cannot be opened.

The project type is not supported by this installation.

It’s wonderful, isn’t it, that this message tells us next to nothing? Which project type is unsupported? There’s no way to tell, as the Property Dialog for an unloaded project doesn’t tell us much.

The reason, it seems, is that the unsupported project is probably built against a different MVC version.

And the solution is to hack the project file:

  1. Right click and select ‘Edit …’.
  2. Replace the whole tag with the following:
    <ProjectTypeGuids>{603c0e0b-db56-11dc-be95-000d561079b0};{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>

Or, as in my case, download and install ASP.NET MVC :-(

Monday, June 22, 2009

Monday, May 11, 2009

Essential Firefox add-ons

Firebug to debug javascript and copy-paste the designer’s precious html code.

QuickProxy provide a single-click button on the statusbar to quickly switch proxy on and off.

Tab To Window and Tabs Open Relative because I like the way Google Chrome handle tabs. I dislike how Chrome does not support Firebug-like features. I could install Firebug Lite, though.

Wednesday, May 06, 2009

Plugin: Insert Code for Windows Live Writer

Nice code formatter for WLW.

Cool jQuery trick. Or why it pays off to know how things really work

So we have this search against a CRM provider which could have been a lot faster, to say the least. It’s just that we don’t know if this CRM is inherently slow, or us who are inherently incompetent.

Anyways, when we can’t speed things up, we give the user a slightly better experience like an animated dog jumping around, or the more professional spinner.

This being a web application, I dreaded this task. I really didn’t want to change a well functioning search form (except for the lack of speed) into a conglomerate of ajax calls and dom manipulations. Luckily, a coworker volunteered.

Today he asked for a review. Browsing his code, I noted he had begun his journey down that dreaded path. Then we had a moment of insight:

“This is a regular post, right? And the browser will get nothing in return until the search is done, yes?

So all we need is to show that flashy spinner while we wait for the results.

Why not use jQuery to hook up the proper event and make the spinner visible when we post?”

And so we did:

$(“#submitButton”).click( function() { $(“#spinner”).show(); } );




Works like a breeze. At least in Firefox.

Monday, May 04, 2009

Connecting an external drive with unknown partition type via USB

Connecting an external drive with unknown partition type via USB doesn’t work too well. But it wasn’t until I discovered the unknown, or rather blank, partition type in Disk Manager, I realized I could just delete the existing volume and create a new one.

Sunday, May 03, 2009

The Lenovo touch pad must be deactivated in BIOS

I am not exactly in love with the touch pad, and whenever I repave my computer I always forget how to permanently disable it.

It’s possible to deactivate it from the mouse settings, however this setting is not honored on a system restart. And that happens a lot on Windows 7 beta.

No, I have to get rid of this abomination from within the BIOS settings.

Sunday, September 16, 2007

A clever captcha

I was posting a comment at James Newkirk's, and noticed a clever captcha implementation:
First, you don't see it. You write your comment and publish it. I guess all the spam bots are satisfied by now.
Then, after publishing, we are presented a final step, the captcha. This after-publish-step solves a sometimes experienced problem of mine: Lost comments due to captcha problems.