在运行时为操作方法创建适当的参数?

发布于 2024-11-08 03:24:09 字数 322 浏览 0 评论 0原文

情况是这样的。

我有一个操作方法,它返回一个文件,在“文档”控制器中称为“GetDocument”。它有一个文档类型的参数,其中包含文档路径、标题、类型等。

我有一个实体的视图,其中附加了一些文档。例如,带有附加文档的新闻报道。在此视图中,我显示了最终用户应该能够下载的文档的各种链接。

问题是:如何创建这样的链接,将正确的“Document”对象传递给“GetDocument”操作方法?

编辑:我不想向用户显示文档的完整路径。事实上,我希望将文档存储在 App_Data 文件夹中,这样就无法下载它们。

谢谢!

Here is the situation.

I have an action method, which returns a file, called "GetDocument" in "Documents" controller. It has one parameter of type Document, which contains document path, title, type etc.

I have a View for an entity, which has some documents attached with it. For example, a news story, with attached documents. On this view I show various links for the documents which the end user should be able to download.

The question is: How do I create such links which pass proper "Document" object to the "GetDocument" Action Method?

Edit: I do not want to show full path of the document to the user. In fact, I would like that I store documents in App_Data folder, so that they cannot be downloaded otherwise.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-11-15 03:24:09

您可以使用 ActionLink:

@Html.ActionLink(
    "Download", 
    "GetDocument", 
    new {
        path = "report.pdf",
        type = "application/pdf"
    }
)

然后:

public ActionResult GetDocument(Document doc)
{
    var appData = Server.MapPath("~/App_Data");
    var file = Path.Combine(appData, doc.Path);
    file = Path.GetFullPath(file);
    if (!file.StartsWith(appData))
    {
        // Ensure there are no cheaters trying to read files
        // outside of the App_Data folder like "../web.config"
        throw new HttpException(403, "Forbidden");
    }
    if (!File.Exists(file))
    {
        return HttpNotFound();
    }
    return File(file, doc.Type, Path.GetFileName(file));
}

You could use an ActionLink:

@Html.ActionLink(
    "Download", 
    "GetDocument", 
    new {
        path = "report.pdf",
        type = "application/pdf"
    }
)

and then:

public ActionResult GetDocument(Document doc)
{
    var appData = Server.MapPath("~/App_Data");
    var file = Path.Combine(appData, doc.Path);
    file = Path.GetFullPath(file);
    if (!file.StartsWith(appData))
    {
        // Ensure there are no cheaters trying to read files
        // outside of the App_Data folder like "../web.config"
        throw new HttpException(403, "Forbidden");
    }
    if (!File.Exists(file))
    {
        return HttpNotFound();
    }
    return File(file, doc.Type, Path.GetFileName(file));
}
眼前雾蒙蒙 2024-11-15 03:24:09

为什么在调用操作时您的控制器需要了解完整文档?你的文档肯定有 ID。获取 id,向存储库询问域对象,然后获取文件。

why would your controller need to know about the full document when the action is called? surely your documents have ids. take the id, ask your repository for the domain object, and then get the file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文