[英]Swap to Development Mode
嗨,我開始使用 IMailservice 后一直收到此錯誤
開發模式 切換到開發環境會顯示有關所發生錯誤的詳細信息。 不應為已部署的應用程序啟用開發環境。 它可能導致向最終用戶顯示來自異常的敏感信息。 對於本地調試,通過將 ASPNETCORE_ENVIRONMENT 環境變量設置為 Development 並重新啟動應用程序來啟用開發環境。
我的申請還沒有發布。 我在 launchSettings.json 中的環境變量設置為 Development,我還使用>setx
設置它,我還檢查了 project>properties>debug。 我的郵件服務
{
Task SendEmailAsync(string toEmail, string subject, string content);
}
public class SendGridMailService : IMailService
{
public async Task SendEmailAsync(string toEmail, string subject, string content)
{
var apiKey = ConfigurationManager.AppSettings["SendGrid"];
var client = new SendGridClient(apiKey);
var from = new EmailAddress("somemail@gmail.com", "MyApplication");
var to = new EmailAddress(toEmail);
var msg = MailHelper.CreateSingleEmail(from, to, subject, content, content);
var response = await client.SendEmailAsync(msg);
}
}
這是我的注冊操作
[HttpPost]
public async Task<IActionResult> Registers(Registration registration)
{
if (ModelState.IsValid)
{
var user = new IdentityUser
{
UserName = registration.Email,
Email = registration.Email
};
var result = await userManager.CreateAsync(user, registration.Password);
if (result.Succeeded)
{
var token = await userManager.GenerateEmailConfirmationTokenAsync(user);
var confirmationLink = Url.Action("ConfirmEmail", "Accounts", new { userId = user.Id, token }, Request.Scheme);
await mailService.SendEmailAsync(registration.Email, "Account Confirmation", confirmationLink);
ViewBag.ErrorTitle = "Registration Successful";
ViewBag.ErrorMessage = "Before you can login, Please Confirm your email. Thank you";
return RedirectToPage("/Error");
}
foreach(var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
return View(registration);
}
我還將我的啟動類更改為
if (env.IsDevelopment() || env.IsProduction() || env.IsStaging())
{
app.UseDeveloperExceptionPage();
}
launchSettings.json 文件僅在您在 Visual Studio 或使用 dotnet cli 中運行/調試項目時使用,因此在這種情況下,您無需在其他任何地方設置 ASPNETCORE_ENVIRONMENT 變量。
將應用程序部署到 IIS 后,可以通過幾種不同的方式設置 ASPNETCORE_ENVIRONMENT,包括您提到的一種方式 - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-3.1#window
對我來說,當您運行 setx 命令並且您設置了不正確的值時,這看起來像是一個錯字。 確保 ASPNETCORE_ENVIRONMENT 在系統級別上是正確的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.