Sunday 21 July 2013

Setting up HttpHandler in IIS 7 or IIS 8 integrated mode

I spent hours troubleshooting a 404 problem with it and it turned out to be one of those doh! moments when I finally figured it out. I thought it was worth posting something about it here because during my research into the matter I found a lot of scattered and contradicting information about it on the web. Some said you have to strongly name and sign your assembly and deploy to the GAC in order to use a custom httphandler with IIS, which is not true, and others had some downright way of going about it.

In any case it's very very simple. All you need to do is declare your handler under web.config under <system.webserver> like this:

<system.webserver>
 <add name="MyHandlerClass" path="MyHandlerMethod" verb="GET" type="FullNamespace.ClassName, FullNamespace.SyncServer" resourceType="Unspecified" preCondition="integratedMode" />
</system.webserver>

Notes:

1)You must declare this inside the <system.webserver> not <systerm.web> and in fact make sure you do not include a declation for the same handler unser <system.web> otherwise it'll get confused

2)the name must match the name of your HttpHandler class

3)the path is the method in your httphandler class that you want to call which is also part of the path when invoking it

4)type is just the full assembly name for your custom header including the class name for your custom httphandler

5)resourceType is the equivalent to "verify that file exists"in II 6. leaving it as undefined prevents IIS from checking for a file specifically which may be what you need particularly if your handler just maps to a path rather than serving a file

6)this tells IIS to run the handler in integrated mode

now the most important one, for me, anyway and what kept me troubleshooting this for hours:

7)If it's an MVC project... MAKE SURE TO ADD IT TO YOUR IGNORE ROUTES CONFIGURATION!

Tuesday 2 July 2013

How to calculate the closest date to now in a collection using C#

there are a lot of unnecessarily over-complicated ways out there showing different ways of doing this so I thought I'd post what I think is the simplest and best solution for this.

Just a bit of context first: my scenario is that I want to find out which files in the collection that I have the closest publishing date to today based on the date set on the filet set they belong to.

 var now = DateTime.Now;
                
var closestDiff  = activeFiles.Min(x => Math.Abs((x.Fileset.PublishDate - now).Ticks));

                files = activeFiles.Where(x => Math.Abs((x.Fileset.PublishDate - now).Ticks) == closestDiff  ).ToList();

one important pointer:

make sure to save the DateTime.Now or whatever date you're comparing to in a variable first if you are using a dynamic date like that.

Don't forget that milliseconds count when dealing with datetime so you if you just use that statement inline the code will never match anything since the Ticks result will be different in the next code line and the closestDiff won't match anything


               

Monday 24 June 2013

Unable to start debugging on the web server. IIS does not list a web site that matches the launched URL.


if you're encountering this error it's because you haven't started Visual Studio in Administrator mode. This will particularly be true for Visual Studio 2012 with Windows 8 users.



so start Visual Studio again in Administrator mode and this cryptic error should go away.

Sunday 23 June 2013

Ajax.BeginForm displaying System.Web.Mvc.Html.MvcForm on View

if you're using Ajax.BeginForm() on your view MVC view don't forget to wrap it with a using statement otherwise it will print System.Web.Mvc.Html.MvcForm which is really annoying.

so just make sure that your statement looks like this:

@using(Ajax.BegindForm(....))
{

}

Monday 17 June 2013

Prioritize your wifi connections in Windows 8

Microsoft still refuses us to gives us a GUI to do something as simple as prioritizing what WiFi connections you want prioritized. However, it's easy enough to do.

1)Open command prompt and type: "netsh wlan show profiles"

2)Type the following filling in the parameters with the information displayed on the previous output:
netsh wlan set profileorder name="name of wifi you want to prioritize" interface="name of the wifi interface which you can find on the previous output" priority=1 

obviously replace 1 by whatever priority you want to give that wifi connection.

Thursday 13 June 2013

Handler "ExtensionlessUrlHandler-Integrated-4.0" has a bad module "ManagedPipelineHandler" in its module list

This is most likely due to the second stupid decision of whoever is leading the IIS team at Microsoft (see my previous post for the first one).

If you install IIS 8.0, it will not by default install the ASP.Net 4.5 module with it. It won't even prompt you though it looks like you have set up everything as it's tucked under the Application Development Features which is semi checked so you'd just assumed that it picked sensible defaults. not so.

They have dropped aspnet_regiis so what you have to do is:

1)Go to Control Panel -> Programs and Features
2)Find IIS 8.0
3)Click on Turn Windows Features on/off
4)Expand the Internet Information Services
5)Expand the World Wide Web Services
6)Expand the Application Development Features
7)Tick Asp.Net 4.5 (and whatever else you may need for that matter)

screenshot:




The requested page cannot be accessed because the related configuration data for the page is invalid.

This error usually means that your applicationhost.config which is the base config installed by IIS on your windows/system32 dir which provides default configuration for .net web sites is denying override for a particular configuration section that you are using in your web.config on your web site.

This error has suddenly become very widespread because in IIS 8.0 in Windows 8.0 the default override setting for the <handlers> section is Deny which means you can't have it in your web.config. Considering that every web site template generated by Visual Studio has the <handlers> section with a few things in it, I can only fathom the logic behind this decision, other than consider it a blunder.

In any case, easy enough to fix. 

1)navigate to windows\system32\inetsrv\config and open applicationhost.config

2)find the section that you need to use in your web.config (this error usually outputs a helpful message telling which section is erroring) and change the setting from Deny to Allow.