在SCD中访问静态文件

时间:2016-11-15 14:36:28

标签: asp.net-core .net-core

.NET CORE应用程序中,我在wwwroot文件夹中使用静态文件。  在dotnet run运行index.html文件时,localhost:port/文件在SCD平滑显示,但在将应用程序发布为.exe自包含开发包后,运行{{1}生成的文件static files未显示localhost:port/

在浏览器的开发人员屏幕中,我找到了404 error文件未找到。

2 个答案:

答案 0 :(得分:1)

project.json中添加相应的声明:

  "publishOptions": {
    "include": [
      "wwwroot",
      "appsettings.json",
      "web.config"
    ]
  },

答案 1 :(得分:0)

显然.cs以外的文件未打包,需要手动添加到publish文件夹。

我将以下示例发送html文件作为附加电子邮件:

enter image description here

,文件结构为:

project.json

{
  "version": "1.0.0-*",
  "buildOptions": {
    "debugType": "portable",
    "emitEntryPoint": true
 },
"runtimes": {
    "win10-x64": {},
    "win10-x86": {}
},
"dependencies": {
   "NETStandard.Library": "1.6.0",
   "Microsoft.NETCore.Runtime.CoreCLR": "1.0.2",
   "Microsoft.NETCore.DotNetHostPolicy":  "1.0.1",
   "MailKit" : "1.10.0"
 },
"frameworks": {
   "netstandard1.6": { }
 }
}

program.cs

using System;
using System.IO;  // for File.ReadAllText
using MailKit.Net.Smtp;   // for SmtpClient
using MimeKit;  // for MimeMessage, MailboxAddress and MailboxAddress
using MailKit;  // for ProtocolLogger

namespace sendHTMLemail
 {
     public class newsLetter
     {
         public static void Main()
         {
             var message = new MimeMessage();
             message.From.Add(new MailboxAddress("INFO", "info@xx.com.sa"));
             message.To.Add(new MailboxAddress("MYSELF", "myself@xx.com.sa"));
             message.Subject = "Test Email";
             var bodyBuilder = new BodyBuilder();
             string htmlFilePath = "./html-files/msg.html";
             bodyBuilder.Attachments.Add("./html-files/msg.html");
             bodyBuilder.HtmlBody = File.ReadAllText(htmlFilePath);
             message.Body = bodyBuilder.ToMessageBody();

            using (var client = new SmtpClient (new ProtocolLogger ("smtp.log")))
            {
                   client.Connect("smtp.office365.com", 587);
                   try{
                       client.Authenticate("info@xxx.com.sa", "inof@PSWD");
                   }
                   catch{
                       Console.WriteLine("Authentication Faild");
                    }

                    try
                       {
                       client.Send(message);
                    }
                    catch (Exception)
                        {
                             Console.WriteLine("ERROR");
                    }
                    client.Disconnect(true);
            }
       }
   }
 }

以上工作非常正常,publish已经准备好运行以下命令:

dotnet restore

dotnet build -r win10-x64
dotnet build -r win10-x86

dotnet publish -c release -r win10-x64
dotnet publish -c release -r win10-x86

但在执行.exe文件夹中的publish文件时,却发出错误,指出找不到文件pathTo/html-files/msg.html

我将publish文件夹所需的文件夹复制到下面后,一切正常:

enter image description here

注意 如果您不需要用户看到公共/静态文件,那么您可以压缩它们,然后从内存流中读取它们,如here所述