似乎唯一明智的做法是在URL中不允许使用空格。对.NET正确编码的支持似乎有点不稳定:( 自动生成空格时,我将用破折号代替空格,并验证它们仅包含某些字符(字母数字,点,破折号,斜杠)。 我认为,使用它们的最佳方法是将%20存储在数据库中,因为该空间是“不安全的”,然后以通过.NET的W3C验证器的方式对其进行编码似乎很简单。     
URI和URL是两件事,URL是URI的子集。因此,URL对URI具有不同的限制。 要将路径字符串编码为正确的W3C URL编码标准,请使用“ 10”。它将添加您要编码的空格。 您应该以最有用的形式存储URL。在将它们编码为符合URL的格式之前,将它们称为URI可能会很有用,但这只是语义,可以帮助您使设计更加清晰。 编辑: 如果您不喜欢被编码的斜杠,可以通过将编码的ѭ11replacing替换为更简单的
/
来对它们进行“解码”,这很简单:
var path = \"/tags/ASP.NET MVC\";
var url = HttpUtility.UrlPathEncode(path).Replace(\"%2f\", \"/\");
    
我没有使用过它,但是UrlPathEncode听起来像它可以提供您想要的东西。   您可以使用   UrlEncode()方法或   UrlPathEncode()方法。然而   方法返回不同的结果。的   UrlEncode()方法转换每个空间   字符到加号(+)。的   UrlPathEncode()方法转换每个   将空格字符插入字符串\“%20 \”,   代表在   十六进制表示法。 编辑:javascript方法encodeURI将使用%20而不是+。添加对Microsoft.JScript的引用,然后调用GlobalObject.encodeURI。在这里尝试过该方法,您将得到想要的结果:     
不久前我问过类似的问题。简短的答案是用\“-\”替换空格,然后再次退出。这是我使用的来源:
private static string EncodeTitleInternal(string title)
{
        if (string.IsNullOrEmpty(title))
                return title;

        // Search engine friendly slug routine with help from http://www.intrepidstudios.com/blog/2009/2/10/function-to-generate-a-url-friendly-string.aspx

        // remove invalid characters
        title = Regex.Replace(title, @\"[^\\w\\d\\s-]\", \"\");  // this is unicode safe, but may need to revert back to \'a-zA-Z0-9\', need to check spec

        // convert multiple spaces/hyphens into one space       
        title = Regex.Replace(title, @\"[\\s-]+\", \" \").Trim(); 

        // If it\'s over 30 chars, take the first 30.
        title = title.Substring(0, title.Length <= 75 ? title.Length : 75).Trim(); 

        // hyphenate spaces
        title = Regex.Replace(title, @\"\\s\", \"-\");

        return title;
}
    

要回复问题请先登录注册