【问题标题】:Optional HttpPostedFileBase in ASP.NET MVCASP.NET MVC 中的可选 HttpPostedFileBase
【发布时间】:2013-09-20 03:34:05
【问题描述】:

我有一个用 MVC 编写的 ASP.NET Web API。 我有一个带有一些可选参数的发布操作。

public Guid PostExecution(string Action, List<Guid> ComputersIDs, int Port = 0, string Command = null, string Password = null)

完美运行。现在我需要这个动作来接收一个通过 post 方法发送的可选文件。

所以:

public Guid PostExecution(string Action, List<Guid> ComputersIDs, int Port = 0, string Command = null, string Password = null, HttpPostedFileBase file = null)

有趣的是,当我添加参数HttpPostedFileBase 时,服务器停止响应对此操作的请求,并且只显示错误 500 内部服务器错误。它没有抛出任何异常。使用断点代码不会进入PostExecution

为什么会这样?

如何调试此错误?

是否可以有一个可选的HttpPostedFileBase

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-3 asp.net-web-api


    【解决方案1】:

    所有发布的文件都是可选的!

    你可以通过Request.Files得到它们,你不需要在methods参数中拥有它们

    【讨论】:

    • 请再解释一下。根据stackoverflow.com/a/5193851/909974,我曾经做过类似 public ActionResult Index(HttpPostedFileBase file) 之类的事情
    • 嗯,这就是asp.net mvc的绑定魔法!但是当您确定会有一个文件时,您想使用它,实际上绑定它自己也从 Request.Files 获取文件,并且如果用户不发送任何文件,只需使该方法更易于阅读和遵循具有该特殊名称(输入类型=文件)的文件将找不到操作,并且 404 错误返回到客户端
    猜你喜欢
    • 2013-01-12
    • 1970-01-01
    • 2017-05-26
    • 1970-01-01
    • 2017-10-22
    • 2012-01-23
    • 2016-04-08
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多