是否可以使用T4MVC和异步操作?

时间:2015-11-30 16:21:44

标签: c# asp.net-mvc async-await t4mvc

我的行动:

 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

1 个答案:

答案 0 :(得分:2)

由于异步如何工作而发生错误。您需要在方法中等待任务,以便获得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)