访问 SCD 中的静态文件
Accessing static files in SCD
在 .NET CORE
应用程序中,我使用 wwwroot
文件夹中的静态文件。
虽然 运行 将其设置为 dotnet run
,但 index.html
文件在 localhost:port/
处显示流畅且正常,但在将应用程序发布为 SCD
独立开发包之后,并且运行生成的.exe
文件,static files
处不显示localhost:port/
。
在浏览器的开发者屏幕中,我得到 404 error
找不到文件。
显然 .cs
以外的文件未打包,需要手动添加到 publish
文件夹。
我有以下发送 html 文件作为电子邮件附件的示例:
文件结构为:
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
但是在执行 publish
文件夹中的 .exe
文件时,出现错误,找不到文件 pathTo/html-files/msg.html
。
将所需的文件夹复制到 publish
文件夹后,一切正常:
注意
如果您不需要用户看到 public/static 文件,那么您可以压缩它们,然后从内存流中读取它们,如
所述
在你的project.json
中添加相应的语句:
"publishOptions": {
"include": [
"wwwroot",
"appsettings.json",
"web.config"
]
},
在 .NET CORE
应用程序中,我使用 wwwroot
文件夹中的静态文件。
虽然 运行 将其设置为 dotnet run
,但 index.html
文件在 localhost:port/
处显示流畅且正常,但在将应用程序发布为 SCD
独立开发包之后,并且运行生成的.exe
文件,static files
处不显示localhost:port/
。
在浏览器的开发者屏幕中,我得到 404 error
找不到文件。
显然 .cs
以外的文件未打包,需要手动添加到 publish
文件夹。
我有以下发送 html 文件作为电子邮件附件的示例:
文件结构为:
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
但是在执行 publish
文件夹中的 .exe
文件时,出现错误,找不到文件 pathTo/html-files/msg.html
。
将所需的文件夹复制到 publish
文件夹后,一切正常:
注意
如果您不需要用户看到 public/static 文件,那么您可以压缩它们,然后从内存流中读取它们,如
在你的project.json
中添加相应的语句:
"publishOptions": {
"include": [
"wwwroot",
"appsettings.json",
"web.config"
]
},