Best way to format a query string in an asp.net mvc url?
I've noticed that if you sent a query string routevalue through asp.net mvc you end up with all whitespaces urlencoded into "%20". What is the best way of 开发者_运维技巧overriding this formatting as I would like whitespace to be converted into the "+" sign?
I was thinking of perhaps using a custom Route object or a class that derives from IRouteHandler but would appreciate any advice you might have.
You could try writing a custom Route:
public class CustomRoute : Route
{
public CustomRoute(string url, RouteValueDictionary defaults, IRouteHandler routeHandler)
: base(url, defaults, routeHandler)
{ }
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
var path = base.GetVirtualPath(requestContext, values);
if (path != null)
{
path.VirtualPath = path.VirtualPath.Replace("%20", "+");
}
return path;
}
}
And register it like this:
routes.Add(
new CustomRoute(
"{controller}/{action}/{id}",
new RouteValueDictionary(new {
controller = "Home",
action = "Index",
id = UrlParameter.Optional
}),
new MvcRouteHandler()
)
);
精彩评论