【问题标题】:Swap to Development Mode切换到开发模式
【发布时间】:2020-07-04 10:43:53
【问题描述】:

嘿, 我开始使用 IMailservice 后一直收到此错误

开发模式 切换到开发环境会显示有关所发生错误的详细信息。 不应为已部署的应用程序启用开发环境。它可能导致向最终用户显示来自异常的敏感信息。对于本地调试,通过将 ASPNETCORE_ENVIRONMENT 环境变量设置为 Development 并重新启动应用程序来启用开发环境。

我的申请尚未发布。 我在 launchSettings.json 中的环境变量设置为 Development, 我还使用>setx从命令行设置它, 我还检查了项目>属性>调试。 我的邮箱服务

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

【问题讨论】:

    标签: c# asp.net-core


    【解决方案1】:

    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#windows

    对我来说,当您运行 setx 命令并且您设置了不正确的值时,这看起来像是一个错字。确保 ASPNETCORE_ENVIRONMENT 在系统级别上是正确的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2011-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-19
      • 2021-01-17
      相关资源
      最近更新 更多