您可以通过使用继承自 Route 的自定义路由来解决此问题。就像是:
public class UserNameRoute : Route
{
public override RouteData GetRouteData(HttpContextBase httpContext) {
var routeData = base.GetRouteData(httpContext);
// Not sure if you need to set routeData.Values["username"] too
// to make sure the route catches on
routeData.DataTokens["username"] = DecodeUserName(routeData.Values);
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values) {
values["username"] = EncodeUserName(requestContext.RouteData.DataTokens);
return base.GetVirtualPath(requestContext, values);
}
}
然后你“只是”必须实现 DecodeUserName 和 EncodeUserName。