简体   繁体   English

等待mvc4不起作用

[英]await mvc4 does not work

I have a problem with my code, await key word does not work in asp.net mvc4, I do not know what is the problem but when I wrote it the .net framework did not color it by blue!! 我的代码有问题,等待关键字在asp.net mvc4中不起作用,我不知道这是什么问题,但是当我编写它时,.net框架并未将其涂成蓝色!

this is my code 这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using workflow.Authorize;
using System.Web.Script.Serialization;
using workflow.DataHolders;
using workflow.Models;
using workflow;
using System.Net;
using System.Net.Mail;

    [AllowAnonymous]
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult ValidateAndSignUp(ForCompany newCompany)
    {
        if (ModelState.IsValid)
        {
            company NewCompany = new company();
            TryUpdateModel(NewCompany);
            context.companies.Add(NewCompany);
            int processStatus= context.SaveChanges();
            if (processStatus != 0)
            {
                MailMessage message = new MailMessage();
                message.To.Add(newCompany.Email);
                message.Subject = "You have created new account";
                message.Body = "Hi, Welcom to HWorkflow website this email is to approve your account and to creat the first user, please click on the link below to approve your account and to creat your first user";
                message.From = new MailAddress("HWorkflowTeam@hworkflow.com");
                message.IsBodyHtml = true;

                var credential = new NetworkCredential
                {
                    UserName = "hworkflowteam",  // replace with valid value
                    Password = "No-access"  // replace with valid value
                };
                var smtp = new SmtpClient();
                smtp.Credentials = credential;
                smtp.Host = "mail.hworkflow.com";
                smtp.Port = 25;
                smtp.EnableSsl = true;
                await smtp.SendMailAsync(message);

            }
            return RedirectToAction("index", "Home");
        }
        else
        {
            return View("signUp", newCompany);
        }
    }

so please could anyone help me to solve this problem. 所以请任何人帮我解决这个问题。

You need to mark your method as async and to return a Task<T> 您需要将您的方法标记为async并返回Task<T>

public async Task<ActionResult> ValidateAndSignUp(ForCompany newCompany)
{
    await ...
}    

Your Method Signature does not indicate that this is an async / awaitable method. 您的方法签名并不表示这是异步/等待方法。

try 尝试

public async Task<ActionResult> ValidateAndSignUp(ForCompany newCompany)
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM