Web API 2文件上传在开发人员中工作,在生产中提供404

时间:2015-01-14 13:18:14

标签: iis file-upload asp.net-web-api2

我开发了一个fileupload工具(使用angularjs的nervgh fileupload),在开发中运行良好。文件将发送到WebApi上传控制器:

   public class UploadController : ApiController
{       
    public async Task<HttpResponseMessage> PostFormData()
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        string root = HttpContext.Current.Server.MapPath(@"~\" + Properties.Settings.Default.uploadDir);
        var provider = new MultipartFormDataStreamProvider(root);

        var documentNewDir = "";
        var documentRelative = "";
        var fileName = "";

        try
        {
            // Read the form data.
            await Request.Content.ReadAsMultipartAsync(provider);

            // This illustrates how to get the file names.
            foreach (MultipartFileData file in provider.FileData)
            {
                var vergaderDate = Convert.ToDateTime(provider.FormData["vergaderdatum"]);
                var jaar = vergaderDate.Year;
                var maand= vergaderDate.Month.ToString(CultureInfo.CurrentCulture).PadLeft(2, '0');
                var dag =vergaderDate.Day.ToString(CultureInfo.CurrentCulture).PadLeft(2, '0');

                //absolute path to documents new dir
                documentNewDir = HttpContext.Current.Server.MapPath("/") + Properties.Settings.Default.documentsBaseDir + @"\" + jaar + @"\" + maand + @"\" + dag;

                //filename
                fileName = file.Headers.ContentDisposition.FileName.Replace("\"", string.Empty);

                //relativePath and filename
                documentRelative = Properties.Settings.Default.documentsBaseDir + @"/" + jaar + @"/" + maand + @"/" + dag + @"/"+ fileName;


                if(!Directory.Exists(documentNewDir))
                    Directory.CreateDirectory(documentNewDir);
                File.Move(file.LocalFileName, documentNewDir + @"\"+fileName);
            }

            var response = Request.CreateResponse(HttpStatusCode.OK, documentRelative);
            return response;
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }

}

这在开发设置中工作正常,但部署到MS 2012服务器IIS 8.5生产服务器,我在POST API调用api / upload时得到404。

该应用程序支持其他一些在dev / prod环境中工作正常的WebAPI调用....如何开始调查/调试这种情况?

这是Fiddler的电话(遗漏了诉讼数据......):

-----------------------------7df2b09e0350
Content-Disposition: form-data; name="vergaderdatum"

2015-01-30T00:00:00
 -----------------------------7df2b09e0350
Content-Disposition: form-data; name="file"; filename="5.0. Vooraankondiging themabijeenkomstSamen naar Buiten, 17 sept.pdf"
Content-Type: application/pdf

%PDF-1.5
%âãÏÓ

和回复

HTTP/1.1 404 Not Found
Content-Type: text/html
Server: Microsoft-IIS/8.5
X-Powered-By: ASP.NET
Date: Wed, 14 Jan 2015 15:31:08 GMT
Content-Length: 1245

1 个答案:

答案 0 :(得分:1)

我发现了错误! 在生产中,网站被安装为默认网站的子域。在javascript中,我在上传文件的javascript中添加了像/ bisbeheer / api / upload这样的子域名,然后引用了上传方法(/ api / upload)