diff --git a/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs b/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs index 3adbbc1..dddc2a1 100644 --- a/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs +++ b/src/WebApiContrib.Formatting.Xlsx/XlsxMediaTypeFormatter.cs @@ -96,31 +96,35 @@ public override void SetDefaultContentHeaders(Type type, HttpContentHeaders head rawUri = rawUri.Substring(0, queryStringIndex); } - string fileName; - - // Look for ExcelDocumentAttribute on class. - var itemType = util.GetEnumerableItemType(type); - var excelDocumentAttribute = util.GetAttribute(itemType ?? type); - - if (excelDocumentAttribute != null && !string.IsNullOrEmpty(excelDocumentAttribute.FileName)) - { - // If attribute exists with file name defined, use that. - fileName = excelDocumentAttribute.FileName; - } - else - { - // Otherwise, use either the URL file name component or just "data". - fileName = System.Web.VirtualPathUtility.GetFileName(rawUri) ?? "data"; - } - - // Add XLSX extension if not present. - if (!fileName.EndsWith("xlsx", StringComparison.CurrentCultureIgnoreCase)) fileName += ".xlsx"; - - // Set content disposition to use this file name. - headers.ContentDisposition = new ContentDispositionHeaderValue("inline") - { FileName = fileName }; - - base.SetDefaultContentHeaders(type, headers, mediaType); + if (headers.ContentDisposition == null) + { + + string fileName; + + // Look for ExcelDocumentAttribute on class. + var itemType = util.GetEnumerableItemType(type); + var excelDocumentAttribute = util.GetAttribute(itemType ?? type); + + if (excelDocumentAttribute != null && !string.IsNullOrEmpty(excelDocumentAttribute.FileName)) + { + // If attribute exists with file name defined, use that. + fileName = excelDocumentAttribute.FileName; + } + else + { + // Otherwise, use either the URL file name component or just "data". + fileName = System.Web.VirtualPathUtility.GetFileName(rawUri) ?? "data"; + } + + // Add XLSX extension if not present. + if (!fileName.EndsWith("xlsx", StringComparison.CurrentCultureIgnoreCase)) fileName += ".xlsx"; + + // Set content disposition to use this file name. + headers.ContentDisposition = new ContentDispositionHeaderValue("inline") + {FileName = fileName}; + } + + base.SetDefaultContentHeaders(type, headers, mediaType); } [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]