Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 创建指向包含百分号的文件的链接_C#_Asp.net Mvc 3_Url - Fatal编程技术网

C# 创建指向包含百分号的文件的链接

C# 创建指向包含百分号的文件的链接,c#,asp.net-mvc-3,url,C#,Asp.net Mvc 3,Url,我正在用C语言创建一个MVC3 web应用程序。 我必须实现一个搜索屏幕来显示来自SQL数据库的数据和与这些数据对应的图片。 在我的详细信息页面中,我创建了指向此文档的链接: @{ string fullDocumentPath = "~/History/" + Model.PICTURE_PATH + "/" + Model.PICTURE_NAME.Replace("001", "TIF"); } @if (File.Exists(Server

我正在用C语言创建一个MVC3 web应用程序。 我必须实现一个搜索屏幕来显示来自SQL数据库的数据和与这些数据对应的图片。 在我的详细信息页面中,我创建了指向此文档的链接:

        @{
        string fullDocumentPath = "~/History/" + Model.PICTURE_PATH + "/" + Model.PICTURE_NAME.Replace("001", "TIF");
    }
    @if (File.Exists(Server.MapPath(fullDocumentPath)))
    {
        <a href="@Url.Content(fullDocumentPath)" >Click me for the invoice picture.</a>
    }
如何解决此问题?

使用方法转义特殊字符:

@{
   string documentDirectoryPath = "~/History/" + Model.PICTURE_PATH + "/";
   string documentName = Model.PICTURE_NAME.Replace("001", "TIF");
}
@if (File.Exists(Server.MapPath(documentDirectoryPath + documentName)))
{
  <a href="@Url.Content(documentDirectoryPath + Url.Encode(documentName))" >Click me for the invoice picture.</a>
} 

您尝试访问的URL不是URL编码的。您只能使用ASCII字符,因此对于特殊字符,您需要对路径进行URL编码。您可以看到这些字符的列表,以及该列表中相应的ASCII字符:

您可以使用UrlEncode方法将路径字符串转换为URL编码:

如果您想再次解码,可以使用UrlDecode方法:


它创造了类似于http://localhost:49823/BusinessCaseHistory/Details/~%2fHistory%2f132%2f18%2faagn%258ab.TIF,这会导致HTTP错误400-请求错误。对,那么这次你只需要对字符串的名称部分进行编码http://localhost:49823/History/132/18/aagn%258ab.TIF 然后是HTTP错误400-错误请求。如果文件名被更改,则无法找到它。我担心我将不得不重命名数据库中大约402k个文件和记录的所有文件和记录。据我所知,不仅数据库中的名称,而且文件系统中的文件名中都有%的名称,因此,如果您尝试解码名称,您将得到错误的名称。在数据库中,我的文件路径位于“132/21”列中,文件名位于另一列中,如“aagn%8ab.001”。所以,没有要首先解码的url。@迪玛,你是对的。我已经相应地更新了我的答案,但现在它基本上与您的答案相同:@User2185692,您应该只在Model.PICTURE_名称上使用UrlEncode方法-但请记住,在将001替换为TIF后再使用UrlEncode方法。文件名为aagn%8ab.TIF,但在编码后为aagn%258ab.TIF。因此无法找到它。是否没有办法告诉url不要解释这个符号,而只是将其作为普通字符读取?
@{
   string documentDirectoryPath = "~/History/" + Model.PICTURE_PATH + "/";
   string documentName = Model.PICTURE_NAME.Replace("001", "TIF");
}
@if (File.Exists(Server.MapPath(documentDirectoryPath + documentName)))
{
  <a href="@Url.Content(documentDirectoryPath + Url.Encode(documentName))" >Click me for the invoice picture.</a>
}