C# 如果文件存在于asp.net mvc中,则返回链接

C# 如果文件存在于asp.net mvc中,则返回链接,c#,asp.net-mvc,C#,Asp.net Mvc,我需要显示指向文件的链接,但仅当该文件存在时。到目前为止,我已经尝试过扩展UrlHelper、HtmlHelper和MvcHtmlString,但它们似乎都没有给出我需要的结果 我肯定我做错了什么,但我不知道那是什么。UrlHelper扩展看起来非常接近,但是视图将链接呈现为文本而不是锚定 public static class UrlHelperExtensions { public static string Pdf(this UrlHelper helper, string fil

我需要显示指向文件的链接,但仅当该文件存在时。到目前为止,我已经尝试过扩展UrlHelper、HtmlHelper和MvcHtmlString,但它们似乎都没有给出我需要的结果

我肯定我做错了什么,但我不知道那是什么。UrlHelper扩展看起来非常接近,但是视图将链接呈现为文本而不是锚定

public static class UrlHelperExtensions
{
    public static string Pdf(this UrlHelper helper, string fileName, string directory)
    {
        string _fileName = fileName + ".pdf";
        string _directory = directory + "/";

        string root = "~/Content/Templates/";

        string path = HttpContext.Current.Server.MapPath(root + _directory + _fileName);

        if (File.Exists(path))
        {
            return helper.Content("<a href=\"" + root + _directory + _fileName + "\" target=\"_blank\">Download Template</a>");
        }
        else
        {
            return "";
        }            
    }
}

在页面上,而不是实际的链接。

您必须使用
HtmlString
如下所示:

public static HtmlString Pdf(this UrlHelper helper, string fileName, string directory)
{
    string _fileName = fileName + ".pdf";
    string _directory = directory + "/";

    string root = "~/Content/Templates/";

    string path = HttpContext.Current.Server.MapPath(root + _directory + _fileName);

    if (File.Exists(path))
    {
        return new HtmlString(helper.Content("<a href=\"" + root.Replace("~", "") + _directory + _fileName + "\" target=\"_blank\">Download Template</a>"));
    }
    else
    {
        return new HtmlString("");
    }            
}
publicstatichtmlstringpdf(这个UrlHelper,字符串文件名,字符串目录)
{
字符串_fileName=fileName+“.pdf”;
字符串_directory=directory+“/”;
字符串root=“~/Content/Templates/”;
string path=HttpContext.Current.Server.MapPath(根目录+\文件名);
if(File.Exists(path))
{
返回新的HtmlString(helper.Content(“”);
}
其他的
{
返回新的HtmlString(“”);
}            
}

编辑:修复了
根字符串。

还有一个解决方案-

以这种方式扩展(我稍微修改了URL结构,而不是直接链接文件,我让它以目录名和文件名作为参数,点击FileController的DownloadFile操作)

现在确定行动:

    public FileResult Downloadfile(string directoryname, string filename)
    {
        string _fileName = filename + ".pdf";
        string _directory = directoryname + "/";
        string root = "~/Content/";

        string path = Server.MapPath(root + _directory + _fileName);
        return new FileContentResult(System.IO.File.ReadAllBytes(path), _fileName);
    }
现在,当您运行视图时,生成的锚定标记将如下所示-

http://localhost:5738/File/Downloadfile/img/untitled
当您单击它时,您的操作将使用以下值。然后最后下载文件


PS-确保您的代码中有正确的验证。

或者,您可以仅从帮助程序扩展生成路径,然后在视图中转到
。虽然如果您不想为不存在的文件填充链接,这可能不太好。这会返回错误的路径,即,它返回
http://localhost:50467/Item/Details/~/Content/Templates/Retail/1001.pdf
它应该何时返回
http://localhost:50467/Content/Templates/Retail/1001.pdf
它将路径附加到当前位置,而不是根目录。请从您的
根目录中删除
~
字符串以获取路径
helper.Content()
但不适用于
MapPath()
。很好!这就解决了问题。谢谢使用此解决方案比Raidri的解决方案有优势吗?对于我的初学者来说,这似乎需要做更多的工作才能获得相同的结果。@Jason,在生产环境中,我们不提供对文件的直接访问,而是通过一些处理程序传递文件,如我所示。一个关键的例子是文件窃听,如果你打开文件,那么任何网站都可以链接你的文件,并通过为你花费带宽来驱动所有不必要的流量到你的网站和那里。啊,我明白了!我会选择Raidri的答案,因为它明确地给出了我所要求的,但我认为我最终会使用你的解决方案。多谢各位@杰森,没问题。我很高兴我的回答帮助你学习。
routes.MapRoute(
    name: "DownloadfileRoute",
    url: "{controller}/{action}/{directoryname}/{filename}",
    defaults: new { controller = "FileController", action = "DownloadFle" }
);
    public FileResult Downloadfile(string directoryname, string filename)
    {
        string _fileName = filename + ".pdf";
        string _directory = directoryname + "/";
        string root = "~/Content/";

        string path = Server.MapPath(root + _directory + _fileName);
        return new FileContentResult(System.IO.File.ReadAllBytes(path), _fileName);
    }
http://localhost:5738/File/Downloadfile/img/untitled