0

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);
asked Feb 2, 2015 at 21:31
5
  • 1
    that's not url encoding. If you url encode the actual file name, you'd see %20 where spaces are. I think you're doing something to cause that. Commented Feb 2, 2015 at 21:38
  • what's your url route definition look like? Commented Feb 2, 2015 at 21:39
  • please see url route in edit Commented Feb 2, 2015 at 21:41
  • And we also need to see the razor code for this view, and any templates you may use. Commented Feb 2, 2015 at 21:46
  • please see the simplified template Commented Feb 2, 2015 at 21:52

1 Answer 1

1

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;
answered Feb 2, 2015 at 21:55
1
  • 2
    I cannot use @rsImg because of HTML tags in rsImg string, but it works even with @Html.Raw(rsImg). Thanks Commented Feb 2, 2015 at 22:13

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.