Rewrite URL and HttpHandler problems on IIS with certain character types

by APIJunkie 7/30/2008 12:01:00 AM

Trying to rewrite URL's or custom handle URL's on IIS 6/7 can lead to HTTP errors on the IIS level.

The problem is that some characters like a question mark (?) are considered illegal by IIS when they are a part of a URL file name or path.

Example:

http://www.mydomain.com/MyUrlWithQMark?.ashx

Will produce: HTTP Error 404 - File or directory not found, even if there is an HTTP handler defined for that file type.

Since those characters are not allowed by IIS, requests containing those characters are blocked on the Http.sys level before they ever reach the appropriate HttpHandler.

Unfortunately this means that ASP.Net code does not get a chance to handle URL's that contain characters dimmed illegal by IIS.

Currently the only solution is to change IIS registry settings that effect the way Http.sys handles those types of characters.

Specifically you will need to change the AllowRestrictedChars setting from 0 to 1.

The main problem with this solution is that if you don't have access to those registry settings you have a problem!

For example people using shared hosting solutions or shared servers where some sites want to allow this option and some don't will not be able to use this solution.

I hope this will change in future versions of IIS. in my opinion the AllowRestrictedChars option should be configurable per site through an ASP.NET configuration option.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

IIS | .NET | Web Development

From HTTP to HTTPS and back (continued discussion)

by APIJunkie 3/27/2008 11:58:00 PM

A couple of weeks ago I showed an easy way to redirect from HTTP to HTTPS.

Fosiul raised the issue that once you redirect to HTTPS all the pages become secure even the ones we do not need to be secure.

This can lead to some problems like adding additional stress on the web server machine.

To solve this problem we should be able to switch from https to http when ever we detect a page where we do not need secure communications.

One way to do that is to have a function that knows to switch both ways.

Example:

// set protocol to secure or unsecured according to bSecure flag.

// bSecure flag = true -> secure connection

// bSecure flag = false -> unsecured connection

public void setSecureProtocol(bool bSecure)

{

string redirectUrl = null;

// if we want HTTPS and it is currently HTTP

if (bSecure && !Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("http:", "https:");

else

// if we want HTTP and it is currently HTTPS

if (!bSecure && Request.IsSecureConnection) redirectUrl = Request.Url.ToString().Replace("https:", "http:");

//else

// in all other cases we don't need to redirect

// check if we need to redirect, and if so use redirectUrl to do the job

if(redirectUrl!=null)

Response.Redirect(redirectUrl);

}

One way to use the above function is in the page load handler of pages where a transition to or from HTTP or HTTPS should occur.

Putting the function call in the page load handler would make sure that a transition from secure to non secure and vice versa would occur before the rest of the page gets rendered.

Example:

protected void Page_Load(object sender, EventArgs e)

{

// set to HTTPS secure protocol

setSecureProtocol(true);

/// rest of code goes here

/// we will only reach this code in secure mode

}

If you want to be fancier you can use a custom HTTP module to detect and redirect using a variant of the setSecureProtocol function.

Example:

/// <summary>

/// HttpToHttpsRedirector - an http module to detect and redirect from http to https and vice versa

/// </summary>

public class HttpToHttpsRedirector: IHttpModule

{

 

public void Init(HttpApplication context)

{

context.BeginRequest +=
new System.EventHandler(Application_BeginRequest);

}

 

// your BeginRequest event handler.

private void Application_BeginRequest(Object source, EventArgs e)

{

HttpApplication application = (HttpApplication)source;

HttpContext context = application.Context;

// This is where your web site logic should decide if to redirect to http/https.

// The example code below always redirects to https!!!

if (true) setSecureProtocol(context, true);

}

public void Dispose()

{

}

// utility functions

// set protocol to secure or unsecure acording to bSecure flag.

// bSecure flag = true -> secure connection

// bSecure flag = false -> unsecure connection

public void setSecureProtocol(HttpContext context, bool bSecure)

{

string redirectUrl = null;

// if we want HTTPS and it is currently HTTP

if (bSecure && !context.Request.IsSecureConnection) redirectUrl = context.Request.Url.ToString().Replace("http:", "https:");

else

// if we want HTTP and it is currently HTTPS

if (!bSecure && context.Request.IsSecureConnection) redirectUrl = context.Request.Url.ToString().Replace("https:", "http:");

//else

// in all other cases we don't need to redirect

// check if we need to redirect, and if so use redirectUrl to do the job

if (redirectUrl != null)

context.Response.Redirect(redirectUrl);

}

}

Good luck!

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | Web Development

How to fix the server method failed Maximum length exceeded error when using ASP.NET AJAX

by APIJunkie 3/14/2008 1:21:00 AM

If you are using ASP.NET AJAX to create RIA's (Rich Internet Applications) you might run into the maximum length exceeded error.

This error occurs when you are trying to send too much data using ASP.NET AJAX from the client to the server and vice versa.

On the function call stack level this error occurs at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize or at System.Web.Script.Services.RestHandler.InvokeMethod depending if you are sending or receiving data.

The problem is that the default maximum size (102400 characters) does not suffice when you try to transfer a significant amount of data.

To increase the maximum allowed send/receive data you need to change the MaxJsonLength Property.

To change the MaxJsonLength Property you can edit your web.config file.

Look for the following commented section in the web.config file:

 

<!--
Uncomment this line to customize maxJsonLength and add a custom converter -->

<!--

<jsonSerialization maxJsonLength="500">

<converters>

<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>

</converters>

</jsonSerialization>

-->

 

Convert it to something along the following lines (see note below):

 

<!--
Uncomment this line to customize maxJsonLength and add a custom converter -->

<jsonSerialization maxJsonLength="200000"><

converters>

<!--<add name="ConvertMe" type="Acme.SubAcme.ConvertMeTypeConverter"/>--></

converters>

</jsonSerialization>

 

Note that in the example above the max size was set to 200,000 characters.

You should calculate and test the size you will need for your data (there might be a few bytes of overhead so take that into account).

 

Currently rated 3.6 by 14 people

  • Currently 3.571429/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | AJAX | PRB | JSON | How To

Where to download the latest silver light SDK's and tools (Silverlight 2, Blend 2.5 etc.)

by APIJunkie 3/5/2008 5:12:00 AM

It took me some time to find where Microsoft hides the latest Silverlight tools :)

I also noticed that many other people were searching for the latest Microsoft Silverlight 2.0 beta, Silverlight SDK's , Expression Blend, and other tools.

As it turns out you can find a list of all the tools and their download links at:

http://www.microsoft.com/silverlight/resources/tools.aspx

Hope this helps.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | SDK | Silverlight | Web Development

Avoiding some common errors when consuming web services and calling page methods using ASP.NET AJAX extensions

by APIJunkie 2/13/2008 10:14:00 AM
I have compiled a small list of things to remember in order to avoid some common errors when using ASP.NET AJAX extensions.

1. Remember to declare your web services using the ScriptService attribute.

Why bother?

If you forget to do that you will not be able to easily access the web service from the client side script. You will be missing the automatically generated client script that will allow you to call the web service from your client script code.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
   ...

}

}

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/T_System_Web_Script_Services_ScriptServiceAttribute.aspx


2. If you wish to access session state in your web service functions remember to set the EnableSession attribute to true in your web method declaration.

Why bother?

If you forget to do this you will not be able to access session variables inside your web service methods.

Example:

namespace MyNameSpace

 {

[System.Web.Script.Services.ScriptService]
public class MyService : System.Web.Services.WebService
{
    [System.Web.Services.WebMethod(EnableSession=true)]

    public void myFunc()
    {
       Session["XVAR"] = 1;
    }

}

}

More Info:

http://msdn2.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession.aspx

 

3. If you wish to call static page methods from client script remember to set the EnablePageMethods property of the script manager to true.
 

Why ?

If you forget to do that you will not be able to access the static page methods from the client side script.

Example:

<ajaxToolkit:ToolkitScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">

 

More Info:

http://www.asp.net/AJAX/Documentation/Live/mref/P_System_Web_UI_ScriptManager_EnablePageMethods.aspx

 

4. If you wish to mix json.js client side script with the AJAX .NET client side script, use the script manager/script manager proxy Scripts property.

Why ?

You might receive a client side script error.

Example:

ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

For more info you can read my previous post on the subject -> How to use json.js client side JavaScript file inside an AJAX.NET project

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | AJAX | Troubleshoot | JSON | JavaScript

How to use json.js client side JavaScript file inside an AJAX.NET project

by APIJunkie 2/11/2008 8:12:00 AM
Sometimes it is necessary to mix json.js functions like toJSONString() with client script generated by the AJAX.NET library.

If you include the json.js file in your client script you might receive enumValueNotInteger error.

A little digging around landed me on the following helpful discussion at http://forums.asp.net/t/1077290.aspx

Steve Marx raises several options to solve the problem in the above discussion.

To solve my problem I used the script manager Scripts property option.

Example:

<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</ajaxToolkit:ToolkitScriptManager>

Note if you are using master pages or need json.js script inside custom controls you can use the script manager proxy Scripts property instead.

Example:

<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">

<Scripts>

   <asp:ScriptReference Path="json.js" />

</Scripts>

</asp:ScriptManagerProxy>

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | AJAX | JavaScript | JSON

The given assembly name or codebase was invalid. (Exception from HRESULT: 0x80131047)

by APIJunkie 12/20/2007 10:25:00 AM

If you are trying to use an ASP.NET web custom control and you receive the above error.

Make sure that you specify the name of the assembly and not the complete or partial path to the assembly in the register directive.

Example:

Lets assume you have a name space called MyNameSpace and a dll file called MyNameSpace.dll that contains that name space.

The following 2 variations will produce this error:

<%@ Register TagPrefix="MyNS" Namespace="MyNameSpace" Assembly="~/Bin/MyNameSpace.dll" %>

<%@ Register TagPrefix="MyNS" Namespace="MyNameSpace" Assembly="~/Bin/MyNameSpace" %>

The problem is that you must not use the path or file name of the dll in the assembly property of the register directive.

To solve the problem only specify the assembly name in the assembly property of the register directive:

<%@ Register TagPrefix="MyNS" Namespace="MyNameSpace" Assembly="MyNameSpace" %>

 

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | ASP.NET

Fix for Silverlight 1.1 Tools Alpha for Visual Studio 2008 Package Load Failure

by APIJunkie 11/26/2007 11:15:00 PM

Microsoft recently released Visual Studio 2008. Unfortunately those of us who have been waiting for Silverlight integration with the release version of VS 2008 were in for a bad surprise.

The following message appeared when trying to create a new Silverlight project:

"Package Load Failure

Package 'Microsoft.VisualStudio.Silverlight.SLPackage, Microsoft.VisualStudio.Silverlight, Version=9.0.0.0,..."

The reason was that the old version of Silverlight 1.1 Tools Alpha was not compatible with the release version of VS 2008.

The good news is that Microsoft fixed this issue yesterday and released a new version of Silverlight 1.1 Tools Alpha for Visual Studio 2008.

One important thing to note is that  when I installed the new version it failed so I had to run uninstall first and then reinstall the new version to fix the problem.

 

Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

.NET | Silverlight | Visual Studio

Breaking changes in ASP.Net 2.0 HTTP Handlers when moving from IIS5 on Windows 2000 to IIS6 on Windows 2003

by APIJunkie 11/14/2007 11:27:00 PM

 If you use http handlers you might run into the same problem we had.

 On ASP 2.0, IIS 5, Windows 2000 the default http handlers are called automatically when they are not handled by your handler.

 On ASP 2.0, IIS 6, Windows 2003 the default http handlers do not get called and you receive a page error or missing content on your page depending on the content type.

 Example of problem:

 Let’s assume you have an HTTP handler that is configured in your web config file like this:

     <httpHandlers>

      <add verb="GET" path="ab*.jpg" type="WebLib.JPGHTTPHandler,WebLib"></add>

    </httpHandlers>

 The above handler will get called every time an “ab*.jpg” file will be requested.

That is to say a file that starts with “ab” and has the extension “.jpg”.

 On IIS 5 when a file that has a “.jpg” extension and does not start with “ab” is requested, it will be handled by a default ASP.NET handler.

 On IIS6 you will receive an error (in this case. jpg images will be missing from the requested page).

 One way to solve this is to explicitly tell ASP.NET to call the ASP.NET static file handler for every file that has the same extension but is not handled by our handler.

     <httpHandlers>

      <add verb="GET" path="ab*.jpg" type="WebLib.JPGHTTPHandler,WebLib"></add>

      <add verb="GET" path="*.jpg" type="System.Web.StaticFileHandler"></add>

    </httpHandlers>

 Note that the order of handlers in the above example is important. You should put the static file handler after your custom one! If you don’t, your handler will not get called, only the static file handler will get called for all .jpg files.

 You can find more information about the problem and possible solutions at: http://support.microsoft.com/Default.aspx?kbid=909641

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

IIS | .NET

Installing IIS on a 2003 server machine with SP2 and .Net 2.0 already installed

by APIJunkie 11/5/2007 6:20:00 PM


 Unlike the ordeal I had with installing IIS on the XP machine this was much easier.
 The major bug in XP pro that causes the installation to fail does not seem to exist in Windows 2003 SP2.
 
 I did have to reregister .NET (solution 2 on XP) and you will probably need to add authentication if you wish to debug on the server (solution 3 on XP).

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

IIS | .NET

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Name of author

My name is Bacon…James Bacon.

I am an API wars veteran I was wounded by x86 assembly, recovered and moved on to C. I am currently stuck in C++ and sniffing .NET.

I am mainly here to ramble about coding, various API’s, Junkies(me especially) and everything else that happens between coders and their significant other.

E-mail me Send mail


Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
3456789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2008

Sign in