是否可以使用T4MVC和异步操作
本文关键字:异步操作 T4MVC 可以使 是否 | 更新日期: 2023-09-27 18:25:01
我的操作:
public virtual async Task<ActionResult> IdeaDetails(int id)
{
我的路线:
routes.MapRoute("DesignIdeasDetails", "ideadetails/{id}",
MVC.Designer.IdeaDetails().AddRouteValue("id", UrlParameter.Optional)
错误:Instance argument: cannot convert from 'System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' to 'System.Web.Mvc.ActionResult' C:'Work'luxedecorNew'Luxedecor'Luxedecor'App_Start'RouteConfig.cs 19 15 Luxedecor
错误2:System.Threading.Tasks.Task<System.Web.Mvc.ActionResult>' does not contain a definition for 'AddRouteValue' and the best extension method overload 'System.Web.Mvc.T4Extensions.AddRouteValue(System.Web.Mvc.ActionResult, string, object)' has some invalid arguments
由于异步的工作方式而出现错误。您需要等待方法中的Task,以便获得ActionResult,如下所示:
routes.MapRoute("DesignIdeasDetails", "ideadetails/{id}",
(await MVC.Designer.IdeaDetails()).AddRouteValue("id", UrlParameter.Optional)
// or, if the method cannot be used with async/await
routes.MapRoute("DesignIdeasDetails", "ideadetails/{id}",
(MVC.Designer.IdeaDetails().GetAwaiter().GetResult()).AddRouteValue("id", UrlParameter.Optional)