Accessing static files in SCD

2019-03-06 02:07发布

问题:

In the .NET CORE application, I'm using static files in wwwroot folder. while running it as dotnet run the index.html file is displayed smoothly and normal at localhost:port/ but after publishing the app as SCD self-contained development package, and run the .exe generated file, the static files are not displayed at the localhost:port/.

In the developer screen of the browser, I got 404 error that is file not found.

回答1:

Add the corresponding statement in your project.json:

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


回答2:

Apparently the files other than .cs as not packaged, and needed to be added manually to the publish folder.

I've the below example for sending html file as attached email:

and the files structures are:

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);
            }
       }
   }
 }

The above worked very fine, and the publish had been prepared running the below commands:

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

but while executing the .exe file in the publish folder, it gave an error that the file pathTo/html-files/msg.html is not found.

Once I copied the folder required to the publish folder as below, everything worked fine:

NOTE If you do not need the public/static file to be seen by the user, then you can compress them, then read them from memory stream, as explained here