Reputation: 25108
In my ASP.NET MVC application, there is url for example:
<img src="/photo/22316/byt+3%2b1+n.s+(16)/600x400">
which is mapped to action
public void Photo(string sid, string name, int? width, int? height)
{
//read image, resize and return
}
The problem is that the string
byt+3%2b1+n.s+(16)
is automatically decoded by ASP.NET MVC as
byt+3+1+n.s+(9)
and the image cannot be found, because the name of file is
byt 3+1+n.s (9).jpg
I cannot even replace + for space, because only some of plus marks were created by encoding. I need to disable the encoding of parameter of this particular action to solve this issue.
I know that it is better to generate some IDs, but it is late know to change id, I need to preserve the url of images because of compatibility issues and google image search.
The Url routing is:
routes.MapRoute(
null,
"photo/{sid}/{name}/{width}x{height}", // URL
new { controller = "Nemovitost", action = "Foto" }
);
In watch there is:
HttpContext.Request.Url = {http://localhost:31182/photo/22316/byt+3+1+n.s+(9)/600x400}
But in fiddler I can see:
http://localhost:31182/photo/22316/byt+3%2b1+n.s+(9)/600x400
The razor template (simplified):
public static string BuildImageUrl(int realtyId, string filename, int w, int h)
{
filename = Path.GetFileNameWithoutExtension(filename);
return string.Format("/photo/{0}/{1}/{2}x{3}", realtyId, HttpUtility.UrlEncode(filename), w, h).ToLower();
}
var rsImgF = "<img src=\"{0}\">";
....
var rsImg = string.Format(rsImgF, ....,
BuildImageUrl(Model.RealtyId, img.Filename, 600, 400));
@Html.Raw(rsImg);
Upvotes: 0
Views: 2744
Reputation: 8475
You should be fine to remove the url encoding and just use the built in Razor Url encoding, which is applied automatically when using @
:
public static string BuildImageUrl(int realtyId, string filename, int w, int h)
{
filename = Path.GetFileNameWithoutExtension(filename);
// no url encoding here!
return string.Format("/photo/{0}/{1}/{2}x{3}", realtyId, filename, w, h).ToLower();
}
var rsImgF = "<img src=\"{0}\">";
....
var rsImg = string.Format(rsImgF, ....,
BuildImageUrl(Model.RealtyId, img.Filename, 600, 400));
@rsImg;
Upvotes: 1