Trim URL 并在 c# 中获取最后一个斜杠后的名称

Trim the URL and fetch the name after the last Slash in c#

我需要 Trim 我从数据库中获取的 URL 并且需要显示 last 之后的值斜杠 (/)

尝试使用 Trim 函数。

public ActionResult DownloadFile(Int64 NurseId, Int64 PostedJobId, Int64 DocumentId)
    {
        NurseDAL objNurseDAL = new NurseDAL();

        Result objResult = objNurseDAL.FetchDocumentURLfromDocID(DocumentId);


        string path = "D:/TFSProjects/Dot Net Project/NurseOneStop.WebSite/NurseOneStop.WebSite/";
        byte[] fileBytes = System.IO.File.ReadAllBytes(path + objResult.Results.DocumentUrl);
        var URL = objResult.Results.DocumentUrl; //(/Content/Images/UploadedDocuments/20190205131053233.pdf)

        string fileName = filename.extension;
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }

预期的输出应该是来自字符串 URL 的 20190205131053233.pdf:/Content/Images/UploadedDocuments/20190205131053233.pdf

Trim 只能用于 trim 指定字符串中的特定字符。对于这种情况,您可以使用 Path.GetFileName(fileName)。您必须通过添加 using 语句

来引用 System.IO

您可以使用拆分来创建数组

string filename = objResult.Results.DocumentUrl.Split('/').Last;

但是,您只能在分隔符为 / 时使用它。如果是路径,你可能会遇到 \(Windows) 或 /(POSIX),此时使用 System.IO.Path.GetFileName.

可能是个好主意