在 ASP.NET MVC 中禁用 Url 编码
Disable Url Encoding in ASP.NET MVC
在我的 ASP.NET MVC 应用程序中,有 url 例如:
<img src="/photo/22316/byt+3%2b1+n.s+(16)/600x400">
映射到动作
public void Photo(string sid, string name, int? width, int? height)
{
//read image, resize and return
}
问题是字符串
byt+3%2b1+n.s+(16)
被 ASP.NET MVC 自动解码为
byt+3+1+n.s+(9)
找不到图片,因为文件名是
byt 3+1+n.s (9).jpg
我什至无法将+替换为space,因为只有一些加号是通过编码创建的。我需要禁用此特定操作的参数编码来解决此问题。
我知道生成一些id比较好,但是知道改id已经晚了,因为兼容性问题和google图片搜索,我需要保留图片的url。
Url路由是:
routes.MapRoute(
null,
"photo/{sid}/{name}/{width}x{height}", // URL
new { controller = "Nemovitost", action = "Foto" }
);
手表中有:
HttpContext.Request.Url = {http://localhost:31182/photo/22316/byt+3+1+n.s+(9)/600x400}
但是在fiddler中我可以看到:
http://localhost:31182/photo/22316/byt+3%2b1+n.s+(9)/600x400
剃刀模板(简体):
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);
您应该可以删除 url 编码并只使用内置的 Razor Url 编码,它会在使用 @
:
时自动应用
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;
在我的 ASP.NET MVC 应用程序中,有 url 例如:
<img src="/photo/22316/byt+3%2b1+n.s+(16)/600x400">
映射到动作
public void Photo(string sid, string name, int? width, int? height)
{
//read image, resize and return
}
问题是字符串
byt+3%2b1+n.s+(16)
被 ASP.NET MVC 自动解码为
byt+3+1+n.s+(9)
找不到图片,因为文件名是
byt 3+1+n.s (9).jpg
我什至无法将+替换为space,因为只有一些加号是通过编码创建的。我需要禁用此特定操作的参数编码来解决此问题。
我知道生成一些id比较好,但是知道改id已经晚了,因为兼容性问题和google图片搜索,我需要保留图片的url。
Url路由是:
routes.MapRoute(
null,
"photo/{sid}/{name}/{width}x{height}", // URL
new { controller = "Nemovitost", action = "Foto" }
);
手表中有:
HttpContext.Request.Url = {http://localhost:31182/photo/22316/byt+3+1+n.s+(9)/600x400}
但是在fiddler中我可以看到:
http://localhost:31182/photo/22316/byt+3%2b1+n.s+(9)/600x400
剃刀模板(简体):
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);
您应该可以删除 url 编码并只使用内置的 Razor Url 编码,它会在使用 @
:
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;