The tagItem.Name can be anything which I am expecting the Framework will do the proper encoding. if the tag name is "Bussiness & Finance" it is generating Story/Tag/Business%20&%20Finance/1. In VS Web Server it is running okay but when I host it in iis6 or
iis7 it starts to throw 400 - bad request.
Well, ampersands are reserved when it comes to URLs -- they're used to split the query string in parameters. I would bet that's what's going on. You'll probably need to HTML-encode your tagItem.Name property or something similar.
Edit: I meant
URL-encode, cause HTML-encode would just give you &, which would cause the same issue.
Pls take a look at the generated value it is already encoded, so doing the re-enocde will not make any impact. But the issue is why this encoded value throwing html error 400 when hosting in both iis6 and iis7!!!
Using HttpUtility.UrlEncode is a security best practice according to msdn to protect against cross-site scripting attacks, but I haven't seen it used much so I don't know how well it actually works. And even the MVC framework uses both interchangeably, but
since I've started using it I haven't had any problems.
I tried the foolowing prior posting this issue, i have added the result:
Server.UrlEncode(tagItem.Name) -> Story/Tag/Business+%26+Finance/1 -> Html Error 400 - Bad Request
Html.Encode(tagItem.Name) -> Story/Tag/Business%20&%20Finance/1 -> Html Error 400 - Bad Request
HttpUtility.UrlEncode(tagItem.Name) -> Story/Tag/Business+%26+Finance/1 -> Html Error 400 - Bad Request
HttpUtility.UrlPathEncode(tagItem.Name) -> Story/Tag/Business%20&%20Finance/1 -> Html Error 400 - Bad Request
Any other workaround ?
The above highlighted ampersands are exactly what I was referring to. Ampersands are used to delimit query string parameters.
The fact that you're getting HTTP 400 when you correctly encode the ampersand means there's something else going on. I just wanted to clarify what I was saying.
Which version are you using? The latest version automatically UrlEncodes the values in the path. Make sure you are using the MIX release (early march) and remove the call to UrlEncode.
Also, have you checked to see if you are using the ".mvc" extension in your url? It isn't required on IIS 7.0, so if you are you can make sure to remove it. But just verify that your routes take the difference into account.
KaziManzurRa...
Contributor
4802 Points
887 Posts
Bug in Url Building
Feb 10, 2008 11:59 PM|LINK
I am using the following code to build my url:
Url.Action(new { action = "Tag", controller = "Story", name = tagItem.Name, page = 1 })The tagItem.Name can be anything which I am expecting the Framework will do the proper encoding. if the tag name is "Bussiness & Finance" it is generating Story/Tag/Business%20&%20Finance/1. In VS Web Server it is running okay but when I host it in iis6 or iis7 it starts to throw 400 - bad request.
Any help will be appreciated.
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
sliderhouser...
Member
352 Points
121 Posts
Re: Bug in Url Building
Feb 11, 2008 03:12 AM|LINK
Well, ampersands are reserved when it comes to URLs -- they're used to split the query string in parameters. I would bet that's what's going on. You'll probably need to HTML-encode your tagItem.Name property or something similar.
Edit: I meant URL-encode, cause HTML-encode would just give you &, which would cause the same issue.
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Bug in Url Building
Feb 11, 2008 02:05 PM|LINK
Pls take a look at the generated value it is already encoded, so doing the re-enocde will not make any impact. But the issue is why this encoded value throwing html error 400 when hosting in both iis6 and iis7!!!
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
developmenta...
Member
115 Points
65 Posts
Re: Bug in Url Building
Feb 11, 2008 02:45 PM|LINK
I agree, I think you've found a bug. But I believe what sliderhouserules meant was UrlEncode it before you pass it as an argument, like this:
Url.Action(new { action = "Tag", controller = "Story", name = HttpUtility.UrlEncode(tagItem.Name), page = 1 })Or
Using HttpUtility.UrlEncode is a security best practice according to msdn to protect against cross-site scripting attacks, but I haven't seen it used much so I don't know how well it actually works. And even the MVC framework uses both interchangeably, but since I've started using it I haven't had any problems.
KaziManzurRa...
Contributor
4802 Points
887 Posts
Re: Bug in Url Building
Feb 11, 2008 05:55 PM|LINK
I tried the foolowing prior posting this issue, i have added the result:
Server.UrlEncode(tagItem.Name) -> Story/Tag/Business+%26+Finance/1 -> Html Error 400 - Bad Request
Html.Encode(tagItem.Name) -> Story/Tag/Business%20&%20Finance/1 -> Html Error 400 - Bad Request
HttpUtility.UrlEncode(tagItem.Name) -> Story/Tag/Business+%26+Finance/1 -> Html Error 400 - Bad Request
HttpUtility.UrlPathEncode(tagItem.Name) -> Story/Tag/Business%20&%20Finance/1 -> Html Error 400 - Bad Request
Any other workaround ?
Kazi Manzur Rashid
_________________________
Blog: http //kazimanzurrashid.com
Twitter: http://twitter.com/manzurrashid
sliderhouser...
Member
352 Points
121 Posts
Re: Bug in Url Building
Feb 11, 2008 06:01 PM|LINK
The fact that you're getting HTTP 400 when you correctly encode the ampersand means there's something else going on. I just wanted to clarify what I was saying.
ksirg
Member
8 Points
6 Posts
Re: Bug in Url Building
Apr 06, 2008 03:22 PM|LINK
I have the same issue. On my localhost machine it's ok but on iis7 it throw "Bad Request"
I try all methods above and also UrlEncodeUnicode
Is there any solution for that ? Maybe it's iis problem? Have you try with normal asp.net app?
developmenta...
Member
115 Points
65 Posts
Re: Bug in Url Building
Apr 07, 2008 02:49 PM|LINK
Which version are you using? The latest version automatically UrlEncodes the values in the path. Make sure you are using the MIX release (early march) and remove the call to UrlEncode.
developmenta...
Member
115 Points
65 Posts
Re: Bug in Url Building
Apr 07, 2008 03:03 PM|LINK
Also, have you checked to see if you are using the ".mvc" extension in your url? It isn't required on IIS 7.0, so if you are you can make sure to remove it. But just verify that your routes take the difference into account.
ksirg
Member
8 Points
6 Posts
Re: Bug in Url Building
Apr 08, 2008 06:08 PM|LINK
I use mix version and my routes don't have ".mvc" extensions.
And mvc do HtmlAttributeEncode not UrlEncode
HtmlHelper.cs line 121
mvc url bug encoding