【问题标题】:Is it possible to use T4MVC and Asynchronous operations?是否可以使用 T4MVC 和异步操作?
【发布时间】:2016-03-04 08:35:24
【问题描述】:

我的行动:

 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&lt;System.Web.Mvc.ActionResult&gt;' to 'System.Web.Mvc.ActionResult' C:\Work\luxedecorNew\Luxedecor\Luxedecor\App_Start\RouteConfig.cs 19 15 Luxedecor

错误 2:System.Threading.Tasks.Task&lt;System.Web.Mvc.ActionResult&gt;' 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

【问题讨论】:

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


    【解决方案1】:

    错误的发生是由于异步的工作方式。你需要在你的方法中await任务,这样你才能得到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)
    

    【讨论】:

      猜你喜欢
      • 2015-04-24
      • 2012-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-01
      相关资源
      最近更新 更多