您可以编写自定义裁剪路线:
public class CropRoute : Route
{
private static readonly string RoutePattern = "{size}/crop/{fileNameWithoutExtension}.{extension}";
private static readonly string SizePattern = @"^\~(?<width>[0-9]+)x(?<height>[0-9]+)$";
private static readonly Regex SizeRegex = new Regex(SizePattern, RegexOptions.Compiled);
public CropRoute(RouteValueDictionary defaults)
: base(
RoutePattern,
defaults,
new RouteValueDictionary(new
{
size = SizePattern
}),
new MvcRouteHandler()
)
{
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var rd = base.GetRouteData(httpContext);
if (rd == null)
{
return null;
}
var size = rd.Values["size"] as string;
if (size != null)
{
var match = SizeRegex.Match(size);
rd.Values["width"] = match.Groups["width"].Value;
rd.Values["height"] = match.Groups["height"].Value;
}
return rd;
}
}
你会像这样注册:
routes.Add(
new CropRoute(
new RouteValueDictionary(new
{
controller = "Photo",
action = "Thumbnail"
})
)
);
在控制器的Thumbnail
操作中,Photo
您应该在请求时获得所需的一切/~200x400/crop/some_photo.jpg
:
public ActionResult Thumbnail(
string fileNameWithoutExtension,
string extension,
string width,
string height
)
{
...
}