After publishing to a Windows Server 2016 server using the File Sytem method, the method stops working through Visual Studio 2017.

Button action on the view.

<form asp-action="CreateWordFile" asp-route-id="@Model.Mains.ETP_MainId"> <input type="submit" class="btn btn-info" value="СФОРМИРОВАТЬ WORD - ФАЙЛ"> </form> 

The method itself:

  [HttpPost] public async Task<IActionResult> CreateWordFile(int id, ETP_Main main) { main = await _context.ETP_Mains.SingleOrDefaultAsync(m => m.ETP_MainId == id); var webRoot = _env.WebRootPath; //явно указываю путь к папке wwwwroot var pathEtp = Path.Combine(webRoot, "files", "Etp.docx"); //путь к шаблону var pathDoc = Path.Combine(webRoot, "files", "Doc.docx"); //путь к файлу который буду заполнять using (WordprocessingDocument wEtp = WordprocessingDocument.Open(pathEtp, true)) //открываю файл Etp.doc { string docText = null; using (StreamReader sr = new StreamReader(wEtp.MainDocumentPart.GetStream())) //создаю поток чтения { docText = sr.ReadToEnd(); //читаю файл } using (WordprocessingDocument wDoc = WordprocessingDocument.Open(pathDoc, true)) //открываю файл в который буду вносить изменения { using (StreamWriter sw = new StreamWriter(wDoc.MainDocumentPart.GetStream(FileMode.Create))) //создаю поток записи { sw.Write(docText); //записываю текст в новый файл } } } var pathFile = Path.Combine(webRoot, "files", "Doc.docx"); return PhysicalFile(pathFile, "application/msdoc", "Doc.docx"); } 

Mistake:

 http://192.168.0.158/ETP/CreateWordFile/30 HTTP 404 NOT FOUND 

The method should not follow the link ... / CreateWordFile / 30. The method should read / write and start downloading the file.

Libraries in nuget:

 DocumentFormat.OpenXml jQuery Microsoft.AspNetCore.All Microsoft.AspNetCore.Server.IISIntegration Microsoft.VisualStudio.Web.CodeGeneration.Design 
  • one
    This code refers to the DocumentFormat.OpenXml.dll library. Is it installed on the server? - Pavel Mayorov 2:01 pm
  • And you have not done logging? Arrange at least Trace.WriteLine(); according to the method and set the record in the file of these messages to determine where the problem is. - Bulson
  • @PavelMayorov and how should it be installed? DocumentFormat.OpenXml.dll. I connected via NuGEt. After publication, this library is located in the root directory of the published application. - Dmitriy

1 answer 1

The problem turned out to be that by default the docx file has limited rights to modify and write. After allowing all users to make changes and write to the file, the program has worked correctly. The link is very well written how to provide access.

wikihow: remove read only status from MS Word document