属性 有一个 space 并且 Href 忽略了它之后的所有内容

A property has a space and Href is ignorning everything after it

我可以使用直接 link 将上传的文件下载到 Azure,这似乎是最快、最简单的方法。绝不是最安全或最聪明的。但是,当我在 Href 中 link 时,它会忽略所有 space,这意味着如果用户上传名称中带有 space 的文档,它不会正确搜索.我将如何修改我的代码以在搜索时用 % 代替任何空格?

这是我的代码; link点击下载文件;

  <a href=@ViewBag.LinkToDownload@item.DocumentId@item.RevisionId@item.Attachment>Download</a>

控制器

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Download(string id)
    {
        string path = @"https://filestorageideagen.blob.core.windows.net/documentuploader/";

        return View(path + id);
    }

根据你的描述,我测试了这个问题。详情如下,大家可以参考:

测试图片:https://brucechen.blob.core.windows.net/images/hello world.jpg

HttpUtility.UrlEncode or UrlHelper.Encode

此方法会将 space 编码为加号 (+),在编码您的 blob(文件)名称时,您可以获得 https://brucechen.blob.core.windows.net/images/hello+world.jpg,但此时无法访问该文件你的浏览器。

Uri.EscapeUriString

这相当于JavaScript的encodeURIComponent(),可以将space编码为%20,使用此方法可以得到https://brucechen.blob.core.windows.net/images/hello%20world.jpg

根据您的情况,您可以利用 Uri.EscapeUriString 对 url 字符串进行编码。另外,在?之前使用%20,在+之后使用+,具体可以参考这个issue.