Viewresultexecutor changes in ASP. NET Core 2.1

Source: Internet
Author: User
Tags httpcontext

Code that previously worked correctly in ASP. NET Core 2.0:

varServices =httpcontext.requestservices;varExecutor = Services. Getrequiredservice<viewresultexecutor>();varViewengine = Services. Getrequiredservice<irazorviewengine>();varView = Viewengine.getview (NULL,"~/pages/iealert.cshtml",true)?. View;if(View! =NULL){    using(View asIDisposable) {        awaitExecutor. Executeasync (ControllerContext, view, ViewData, TempData,"text/html; Charset=utf-8", $); }    return NewEmptyresult ();} 

The following error occurred after upgrading to ASP. NET Core 2.1:

System.InvalidOperationException:No Service for type ' Microsoft.AspNetCore.Mvc.ViewFeatures.ViewResultExecutor ' have been registered.   At Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService ( IServiceProvider provider, Type servicetype) at   Microsoft.extensions.dependencyinjection.serviceproviderserviceextensions.getrequiredservice[t] ( IServiceProvider provider)

View the definition of Viewresultexecutor discovery Viewresultexecutor implements the Iactionresultexecutor<viewresult> interface to view the latest source generation of ASP. Code to find such registrations:

Services. Tryaddsingleton<iactionresultexecutor<viewresult>, viewresultexecutor> ();

The original viewresultexecutor was also abstracted, so instead of the following code to solve the problem:

varServices =httpcontext.requestservices;varExecutor = Services. Getrequiredservice<iactionresultexecutor<viewresult>> () asViewresultexecutor??Throw NewArgumentNullException ("Executor");varViewengine = Services. Getrequiredservice<irazorviewengine>();varView = Viewengine.getview (NULL,"~/pages/iealert.cshtml",true)?. View;if(View! =NULL){    using(View asIDisposable) {        awaitExecutor. Executeasync (ControllerContext, view, ViewData, TempData,"text/html; Charset=utf-8", $); }    return NewEmptyresult ();}

Viewresultexecutor changes in ASP. NET Core 2.1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.