格式化在asp.net mvc的URL查询字符串的最佳方式? [英] Best way to format a query string in an asp.net mvc url?

查看:270
本文介绍了格式化在asp.net mvc的URL查询字符串的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经注意到,如果你打发你最终urlen codeD中的所有空格asp.net mvc的查询字符串routevalue变成%20。什么是覆盖此格式,因为我想空白的最好办法转换成+符号?

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?

我想也许是使用自定义路由对象或从IRouteHandler但派生类的AP会preciate你可能有任何建议。

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;
    }
}

和注册它是这样的:

routes.Add(
    new CustomRoute(
        "{controller}/{action}/{id}",
        new RouteValueDictionary(new { 
            controller = "Home", 
            action = "Index", 
            id = UrlParameter.Optional 
        }),
        new MvcRouteHandler()
    )
);

这篇关于格式化在asp.net mvc的URL查询字符串的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆