我的行动:
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
答案 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)