Asp.net mvc 如何在ASP.NET MVC控制器中调用异步方法?
我试图在ASP.NET MVC控制器中调用Asp.net mvc 如何在ASP.NET MVC控制器中调用异步方法?,asp.net-mvc,Asp.net Mvc,我试图在ASP.NET MVC控制器中调用async方法,但遇到异常: 此时无法启动异步操作。异步操作只能在异步处理程序或模块内启动,或在页面生命周期中的某些事件期间启动。如果在执行页面时发生此异常,请确保已标记该页面。此异常还可能表示有人试图调用“async void”方法,这在ASP.NET请求处理中通常不受支持。相反,异步方法应该返回一个任务,调用方应该等待它 我的代码: 控制器: [HttpGet] public ActionResult OfferDetailsPage(string
async
方法,但遇到异常:
此时无法启动异步操作。异步操作只能在异步处理程序或模块内启动,或在页面生命周期中的某些事件期间启动。如果在执行页面时发生此异常,请确保已标记该页面。此异常还可能表示有人试图调用“async void”方法,这在ASP.NET请求处理中通常不受支持。相反,异步方法应该返回一个任务,调用方应该等待它
我的代码:
控制器:
[HttpGet]
public ActionResult OfferDetailsPage(string id)
{
// string userid = ClaimsPrincipal.Current.FindFirst(ClaimTypes.Name).Value.ToString();
// var u_role = db.UserDetails.Where(x => x.ADID == userid).FirstOrDefault();
// if (u_role == null)
// {
// return RedirectToAction("Unauthorize", "Account");
// }
// else if (u_role.TypeOfUser.ToLower() != "validator")
// {
// return RedirectToAction("Unauthorize", "Account");
// }
ProductOfferDetails model = new ProductOfferDetails();
AssignedOffersImplementation offerdetail_ = new AssignedOffersImplementation();
DataSet dstProductlist = offerdetail_.GetOfferSubmittedById(id);
if(dstProductlist.Tables[0].Rows[0]["status"].ToString()!="Active")
{
return Content("Offer moved from here..");
}
model.Offer_UOID = dstProductlist.Tables[0].Rows[0]["Offer_UOID"].ToString();
model.OfferId = dstProductlist.Tables[0].Rows[0]["Offer_Id"].ToString();
model.ProductURL = dstProductlist.Tables[0].Rows[0]["Offer_pgURL"].ToString();
// string ProductURL = dstProductlist.Tables[0].Rows[0]["Offer_pgURL"].ToString();
model.DatePublication = Convert.ToDateTime(dstProductlist.Tables[0].Rows[0]["Date_Publication"].ToString());
// model.SoldDate = Convert.ToDateTime(dstProductlist.Tables[0].Rows[0]["Date_Sold"].ToString());
model.OfferStatus = dstProductlist.Tables[0].Rows[0]["OfferStatus"].ToString();
model.SellerComments = dstProductlist.Tables[0].Rows[0]["Seller_Comments"].ToString();
model.Product_Title = dstProductlist.Tables[0].Rows[0]["Product_Title"].ToString();
model.Offer_Sk = dstProductlist.Tables[0].Rows[0]["Offer_Sk"].ToString();
model.ProductSN = dstProductlist.Tables[0].Rows[0]["ProductSN"].ToString();
// model.ImageURL = dstProductlist.Tables[0].Rows[0]["Image_url"].ToString();
model.SNSticker = dstProductlist.Tables[0].Rows[0]["SN_sticker"].ToString();
model.CH_Fashion_Intranet = dstProductlist.Tables[0].Rows[0]["CH_Fashion_Intranet"].ToString();
// model.SNTag = dstProductlist.Tables[0].Rows[0]["SN_Tag"].ToString();
model.SNCard = dstProductlist.Tables[0].Rows[0]["SN_Card"].ToString();
// model.ValidatorComments = dstProductlist.Tables[0].Rows[0]["Validator_Comments"].ToString();
// model.IntialSN = dstProductlist.Tables[0].Rows[0]["Initial_SN"].ToString();
model.SNComments = dstProductlist.Tables[0].Rows[0]["SN_comments"].ToString();
model.SNCertificate = dstProductlist.Tables[0].Rows[0]["SN_Certificate"].ToString();
// model.DateIdentification = dstProductlist.Tables[0].Rows[0]["DateIdentification"].ToString();
model.Offer_UOID = dstProductlist.Tables[0].Rows[0]["Offer_UOID"].ToString();
model.OfferId = dstProductlist.Tables[0].Rows[0]["Offer_Id"].ToString();
model.ProductURL = dstProductlist.Tables[0].Rows[0]["Offer_pgURL"].ToString();
string ProductURL = dstProductlist.Tables[0].Rows[0]["Offer_pgURL"].ToString();
model.DatePublication = Convert.ToDateTime(dstProductlist.Tables[0].Rows[0]["Date_Publication"].ToString());
model.SoldDate = (dstProductlist.Tables[0].Rows[0]["Date_Sold"].ToString());
model.OfferStatus = dstProductlist.Tables[0].Rows[0]["OfferStatus"].ToString();
// model.SellerComments = dstProductlist.Tables[0].Rows[0]["Seller_Comments"].ToString();
model.Product_Title = dstProductlist.Tables[0].Rows[0]["Product_Title"].ToString();
model.Offer_Sk = dstProductlist.Tables[0].Rows[0]["Offer_Sk"].ToString();
// if (dstProductlist.Tables[0].Rows[0]["SNEnteredStatus"ToString() == "True")
// {
// return Content("Offer Already Submitted");
// };
// model.ProductSN = dstProductlist.Tables[0].Rows[0]["ProductSN"].ToString();
// model.ImageURL = dstProductlist.Tables[0].Rows[0]["Image_url"].ToString();
// model.SNSticker = dstProductlist.Tables[0].Rows[0]["SN_sticker"].ToString();
// model.CH_Fashion_Intranet = dstProductlist.Tables[0].Rows[0]["CH_Fashion_Intranet"].ToString();
// model.SNTag = dstProductlist.Tables[0].Rows[0]["SN_Tag"].ToString();
// model.SNCard= dstProductlist.Tables[0].Rows[0]["SN_Card"].ToString();
// model.ValidatorComments = dstProductlist.Tables[0].Rows[0]["Validator_Comments"].ToString();
// model.IntialSN = dstProductlist.Tables[0].Rows[0]["Initial_SN"].ToString();
// model.SNComments = dstProductlist.Tables[0].Rows[0]["SN_comments"].ToString();
// model.SNCertificate = dstProductlist.Tables[0].Rows[0]["SN_Certificate"].ToString();
DataSet dstBlobImages = offerdetail_.BlobImages(model.Offer_Sk);
IEnumerable<Models.BlobImages> _blobs = DataTableExtentions.ToList<Models.BlobImages>(dstBlobImages.Tables[0]);
model.blobs = _blobs;
IEnumerable<ProductOfferDetails> _Productimageslist = DataTableExtentions.ToList<ProductOfferDetails>(dstProductlist.Tables[0]);
TempData["SN_Tag"] = model.SNTag;
TempData["SN"] = model.ProductSN;
TempData["CH_Fashion_Intranet"] = model.CH_Fashion_Intranet;
TempData["SN_Certificate"] = model.SNCertificate;
TempData["SN_Sticker"] = model.SNSticker;
TempData["SN_Comments"] = model.SNComments;
TempData["SN_Card"] = model.SNCard;
string itemcode = string.Empty;
string stylecode = string.Empty;
GetORLIData(model.ProductSN);
MakeRequest(model.ProductSN);
if (Session["itemcode"] != null)
{
itemcode = Session["itemcode"].ToString();
stylecode = Session["stylecode"].ToString();
Session.Remove("itemcode");
Session.Remove("stylecode");
}
// var photo = GetPhoto(model.ProductSN);
ViewBag.itemcode = itemcode;
ViewBag.stylecode = stylecode;
return View(model);
}
请给我建议在ASP.NET MVC控制器中调用异步方法,因为MakeRequest()
声明为async
方法,调用该方法的控制器还必须具有async
修饰符和Task
返回类型,而不是普通的ActionResult
,因为async
方法不能与异常消息中提到的同步处理程序正常工作:
[HttpGet]
public async Task<ActionResult> OfferDetailsPage(string id)
{
// other stuff
// calling async method, this must be await-ed
await MakeRequest(model.ProductSN);
// other stuff
return View(model);
}
异步无效方法真的很糟糕,你应该去了解一下。@DavidG,它们不错,如果你不关心
异步无效的结果,或者你不关心异步无效是否引发异常,你可以使用它们…;)MakeRequest
不需要返回任何内容,它可以是Task
。
[HttpGet]
public async Task<ActionResult> OfferDetailsPage(string id)
{
// other stuff
// calling async method, this must be await-ed
await MakeRequest(model.ProductSN);
// other stuff
return View(model);
}
var result = await MakeRequest(model.ProductSN);