views:

323

answers:

3

Hi,

I have the following route registered;

        routes.MapRoute(
            "LocationsByArea",                                              
            "Locations/{system}/{storage}/{area}",          
            new { controller = "StorageLocation", action = "Index" },
            null
        );

...and the following code in my view;

<%= Html.ActionLink("Platser", "Index", "StorageLocation", new { system = Model.System, storage = Model.Storage, area = item.Name }, null)%>

My problem is when the "area = item.Name" contains a colon, e.g. "Area 4:1". If I click the rendered link I get HTTP-error 400, Bad reqest. I guess I have to encode my area parameter in some way, but I cant figure out how. Any help is apreciated.

Thanks!

A: 

Can you not just use

Server.UrlEnconde(item.Name)

Or am I missing something?

In your routing you may have to use Server.UrlDecde as well although I think It should decode for you on request.

Try using the Routing Debugger to see what the url router is getting passed, then you can see where the decoding needs to happen

Sheff
I have used "area = Url.Encode(item.Name)" but I still get the same error...
Erik Z
I think you will have to UrlDecode back at the other end when reading it back out. Have you tried this?
Sheff
Yes, I have, but I dont reach into the controller.
Erik Z
Sounds like the Decoding needs to happen at routing then, try the routing debugger (I have updated the answer)
Sheff
A: 

ASP.NET 3.5 SP1 and earlier have a number of restrictions on which URLs are valid. In ASP.NET 4 most of these issues have been fixes (or are at least customizable via web.config). I think that the colon character, even when encoded, might not be allowed in ASP.NET 3.5 SP1 and earlier due to security concerns. Allowing colons can be a security problem when performing file checks since they are a little-known syntax for NTFS Alternate Data Streams.

I recommend trying to choose a character other than a colon for these purposes. Maybe a comma, semi-colon, or equal sign might work instead?

Eilon
A: 

The built-in encoding/decoding does not work, so I suggest you roll your own, like this:

namespace MyProject.Helpers
{
    public static class JobNameHelper
    {
        public static string JobNameEncode(string jobname)
        {
            return jobname.Replace(":", "---colon---");
        }

        public static string JobNameDecode(string jobname)
        {
            return jobname.Replace("---colon---", ":");
        }

    }
}
Kjensen