代码之家  ›  专栏  ›  技术社区  ›  ariel

url中的特殊字符表示“错误请求”或“路径中的非法字符”

  •  0
  • ariel  · 技术社区  · 15 年前

    我正在使用javascript请求服务器实时生成的图像。

    该参数在url中传递并被MVC抓取,然后在“id”参数中设置到控制器。这样地:

    Public Function Index(ByVal id As String) As ActionResult
    

    如果我不使用任何特殊字符,比如“?”,这个就可以了或者引用。要发送这些我必须转义或编码这个内容,但我应该怎么做呢?

    我试过javascript escape encodeURIComponent ,但它要么给我“错误请求”,要么给我“路径中的非法字符”

    示例url是 http://localhost/Imagem/Index/Question%3F ,表示“问题?”

    返回“错误请求”

    3 回复  |  直到 15 年前
        1
  •  5
  •   Darin Dimitrov    15 年前

    你会发现 this blog post

    <system.web>
        <httpRuntime 
            requestValidationMode="2.0"
            requestPathInvalidCharacters="&lt;,&gt;,*,%,:,&amp;,\"
            relaxedUrlToFileSystemMapping="true"
        />
        ...
    </system.web>
    
    <system.webServer>
        <security>
            <requestFiltering allowDoubleEscaping="true" />
        </security>
        ...
    </system.webServer>
    

    就我个人而言,我建议您避免在这样的url中使用特殊字符。可以使用映射函数将它们转换为其他字符,也可以仅在查询字符串参数中使用它们: http://localhost/Imagem/Index?id=Question%3F

        2
  •  0
  •   swapneel    15 年前

    您正在尝试访问服务器在运行时创建的映像文件。

    在上面的问题中,您试图访问图像文件作为问题?

    -路径中有非法字符。

        3
  •  0
  •   ariel    15 年前

    谢谢你的回复,但我通过使用 escape % 我稍后会在服务器代码中将字符改回%并取消自己的scape。

    myImg.src = "../../Imagem/Index/" + escape(myValue).replace(/\%/g, "_-_");
    

    在控制器中:

    Public Function Index(ByVal id As String) As ActionResult
        id = id.Replace("_-_", "%")
        id = HttpUtility.UrlDecode(id, System.Text.Encoding.Default())
        Return New ImageResult(CreateImage(id))
    End Function