4

I need to invoke a view component in a service class (not a controller). My aim to render the view component as a string and then process the HtmlString.

I can find help to render a view component in a controller where ActionContext is available but I cannot call my service from a controller. Any help please? I am happy to use Mock and default dependency injection.

Currently, I am using mvc 6 beta 2. Here is the code of my class

public class MyClass
{
    Microsoft.AspNet.Mvc.DefaultViewComponentHelper viewComponentHelper = null;
    Microsoft.AspNet.Mvc.Razor.RazorView razorView = null;

    public MyClass(IViewComponentSelector selector, IViewComponentInvokerFactory factory,
        IRazorPageFactory razorPageFactory, IRazorViewEngine razorViewEngine, IRazorPageActivator pageActivator,
        IViewStartProvider viewStartProvider)
    {
        viewComponentHelper = new DefaultViewComponentHelper(selector, factory);
        razorView = new RazorView(razorViewEngine, pageActivator, viewStartProvider, null, true);
    }

    public void MyAction
    {

        var view = new Mock<IView>().Object;    

        var actionContext = new ActionContext(new RouteContext(new DefaultHttpContext()), new ActionDescriptor());
        var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider(), new ModelStateDictionary());
        var viewContext = new ViewContext(actionContext, view, viewData, TextWriter.Null);

        viewComponentHelper.Contextualize(viewContext);
        var result = viewComponentHelper.Invoke("MyView").ToString();
    }
}

I am getting following exception

   at Microsoft.Framework.DependencyInjection.ServiceProviderExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.AspNet.Mvc.DefaultViewComponentActivator.<>c__DisplayClass6.<CreateActivateInfo>b__7(ViewContext viewContext)
   at Microsoft.AspNet.Mvc.PropertyActivator`1.Activate(Object view, TContext context)
   at Microsoft.AspNet.Mvc.DefaultViewComponentActivator.Activate(Object viewComponent, ViewContext context)
   at Microsoft.AspNet.Mvc.DefaultViewComponentInvoker.CreateComponent(ViewContext context)
   at Microsoft.AspNet.Mvc.DefaultViewComponentInvoker.InvokeSyncCore(MethodInfo method, ViewContext context)
   at Microsoft.AspNet.Mvc.DefaultViewComponentInvoker.Invoke(ViewComponentContext context)
   at Microsoft.AspNet.Mvc.DefaultViewComponentHelper.InvokeCore(TextWriter writer, Type componentType, Object[] arguments)
   at Microsoft.AspNet.Mvc.DefaultViewComponentHelper.Invoke(Type componentType, Object[] args)
   at Microsoft.AspNet.Mvc.DefaultViewComponentHelper.Invoke(String name, Object[] args)
1
  • Did you find a solution? Commented Apr 13, 2016 at 12:54

1 Answer 1

1

In the ConfigureServices section of your startup file, you need to register your service i.e.:

   public void ConfigureServices(IServiceCollection services)
   {
                // Add MVC services to the services container.
                //services.AddMvc();

                services.AddTransient...
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.