Startup.cs:调用控制器上下文

时间:2019-07-17 03:54:45

标签: c# asp.net-core-identity asp.net-core-mvc-2.0

我担心的是:有一个RecurringJon每天发送一封电子邮件。在静态电子邮件内容中,效果很好。但我的最终用户希望他们的重要物品数量每天发送给他们。我为他们的库存提供了一个控制器,可以比较物料以确定哪些物料处于关键水平。但我无法调用广告资源的_context。

我已经尝试在服务中添加上下文,但是仍然无法正常工作。

ItemRegContext

<?php 
            if(isset($_POST['submit']))  {

                $name = $_POST['name'];
                $matNo = $_POST['mat_number'];
                $age = $_POST['age'];
                $specialty = $_POST['specialty'];
                $gender = $_POST['gender'];

                //after user click the submit button 
                $sql_Select_Stundets = "SELECT * FROM student WHERE name = '$name' ";
                 // query the sql with db connection         
                $result_sql_Select_Stundets = mysqli_query($conn,$sql_Select_Stundets);
                //Now check the row count to verify the output if there is any match
                $rowcount=mysqli_num_rows($result);

                //Now write insert inside if condition
                 if( $rowcount >0 ) {

                    if(!empty($name) && !empty($matNo) && !empty($age) && 
                    !empty($specialty) && !empty($gender)) {

                    $sql = "INSERT INTO `student`(`name`, `UB_number`, `age`, 
                                  `sex`, `specialty`)
                    VALUES ('$name', '$matNo', '$age', '$gender', '$specialty')";
                    $conn->query($sql);
                    bheader("Location: index.php");
                }else{

                    echo "Error: Complete all records";
                     }    

                }else{
                       echo "<script>
                       alert('sorry this name is already available');
                        </script>";
                    }

                 }              
        ?>

Startup.cs

namespace Intranet.Controllers
{
   public class ItemRegController : Controller
   {
     private readonly ItemRegContext _context;

     public ItemRegController(ItemRegContext context) 
     {
        _context = context;
    }
}

在公共空白public void Configure(IApplicationBuilder app, IRecurringJobManager recurringJob) { app.UseHangfireDashboard(); recurringJob.AddOrUpdate("", () => SendEmail(), Cron.Minutely()); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); } public void SendEmail() { var message = new MimeMessage(); var builder = new BodyBuilder(); string msgFromDB = string.Empty; var items = _context.ItemRegs; foreach (ItemReg item in items) { if (item.CritLevel >= item.Qty) { msgFromDB += "<tr>" + "<td>" + item.ItemName + "</td>" + "<td>" + item.ItemDesc + "</td>" + "<td>" + item.ManufName + "</td>" + "<td>" + item.AsstSerial + "</td>" + "<td>" + item.PartNum + "</td>" + "<td>" + item.TypeName + "</td>" + "<td>" + item.CalDate + "</td>" + "<td>" + item.Qty + "</td>" + "<td>" + item.UnitName + "</td>" + "<td>" + item.Remarks + "</td>" + "<td>" + item.LocName + "</td>" + "</tr>"; } //else { // msgFromDB += "<tr><<td colspan='11'>No critical record on the list</td>></tr>"; //} } #region email // reference value from appsettings.json string host = "smtp.some.com"; int port = 333; bool boole = false; string authEmail = "somemail@mail.com"; string authPass = "somepassword"; message.From.Add(new MailboxAddress("somename", "jose.baleros@pttphils.com")); message.To.Add(new MailboxAddress("somemail@mail.com")); message.Subject = "some subjects"; builder.HtmlBody = string.Format(@" <p> Critical Stocks for today " + DateTime.Now.ToString() + " </p>" + "<table border='1'> " + "<thead>" + " <tr> " + " <th> Name </th>" + " <th> Item Description </th>" + " <th> MFR </th>" + " <th> Asset/SN </th>" + " <th> P/N </th>" + " <th> Type </th>" + " <th> CAL Date </th>" + " <th> QTY </th>" + " <th> Unit </th>" + " <th> Remarks </th>" + " <th> Location </th>" + " </tr>" + " </thead>" + " <tbody>" + msgFromDB + "</tbody> " + "</table>" + "<br />" + "<br />"); message.Body = builder.ToMessageBody(); using (var client = new SmtpClient()) { client.Connect(host, port, boole); client.Authenticate(authEmail, authPass); client.Send(message); client.Disconnect(true); } } SendEmail()上 _context应该获得ItemRegContext的列表

1 个答案:

答案 0 :(得分:0)