我正在我的Web API中构建一个控制器,允许上传文件,特别是.MP4文件,最终将从我的Unity3D应用程序发布。但是现在我无法接受任何类型的文件而且我一直得到405个结果。
我正在使用MS文档页面中的代码(https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/sending-html-form-data-part-2)除了路径之外没有真正改变它的任何内容。只是试着让它在我的机器上本地工作。调试中没有断点,因为它是405ing:
[HttpPost]
public async Task<HttpResponseMessage> Post()
{
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
}
string root = HttpContext.Current.Server.MapPath("~/videos");
var provider = new MultipartFormDataStreamProvider(root);
try
{
// Read the form data.
await Request.Content.ReadAsMultipartAsync(provider);
// This illustrates how to get the file names.
foreach (MultipartFileData file in provider.FileData)
{
Trace.WriteLine(file.Headers.ContentDisposition.FileName);
Trace.WriteLine("Server file path: " + file.LocalFileName);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
catch (System.Exception e)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
}
}
这是我的配置中的system.webserver:
<system.webServer>
<modules>
<remove name="WebDAVModule" />
</modules>
<handlers>
<remove name="WebDAV" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ResultHandler" path="*.mp4" verb="POST" type="FileUploadApplication.ResultHandler" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
来自我的global.asax:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
GlobalConfiguration.Configuration.Routes.MapHttpRoute("Default",
"{controller}/{id}",
new { id = RouteParameter.Optional });
MediaTypeFormatter());
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
}
}
请求标题:
Content-Type: multipart/form-data; boundary=-------------------------
acebdf13572468
Host: localhost:65056
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Length: 383829
是否有其他需要改变的地方?就像在服务器上的IIS中启用特定文件的上传一样?我之前已经构建了一个简单的Post方法来发布图像,并且不需要做任何类似的事情。
任何帮助都非常感谢!
编辑:我用[HttpPost]修饰了Post()方法,但它仍然无效。我还添加了我使用过的请求标头。
同样在响应标题中,我得到了这个:
Security:
Allow: GET, HEAD, OPTIONS, TRACE