Surendra Sharma

Surendra Sharma

Search This Blog

Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, January 8, 2018

Fix : Duplicate controller Issue in Sitecore Helix project

If you are working in Sitecore Helix Framework and going to call action method like  http://myinstance/api/sitecore/accounts/RenderResetPassword, you may get error like

Server Error in '/' Application.

Multiple types were found that match the controller named 'accounts'. This can happen if the route that services this request ('api/sitecore/{controller}/{action}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.

The request for 'accounts' has found the following matching controllers:
Feature.Abc.Accounts.Controllers.AccountsController
Feature.Xyz.Accounts.Controllers.AccountsController

It’s because of duplicate controller name in Abc and Xyz project.

To fix this, we have to register custom MVC routes in Sitecore. For this, I created one custom processor file “RegisterCustomRoute.cs” to initialize pipeline.

You have to register your duplicate controller as

    public class RegisterCustomRoute
    {
        public virtual void Process(PipelineArgs args)
        {
            RouteTable.Routes.MapRoute
                ("AbcAccount",
                "abc/{controller}/{action}/{id}",
                new { controller = "Accounts", id = UrlParameter.Optional },
                new[] { "Feature.Abc.Accounts.Controllers" }
                );

            RouteTable.Routes.MapRoute
                ("XyzAccount",
                "xyz/{controller}/{action}/{id}",
                new { controller = "Accounts", id = UrlParameter.Optional },
                new[] { "Feature.Xyz.Accounts.Controllers" }
                );
                        }
    }

Here I have append “abc” and “xyz” to separate the URL for account and specified their Namespace as last parameter in MapRoute() method.  Now you can call new URL as

With this you don’t need to put “api/sitecore” in URL now for accounts controller.

I registered this Processor entry in config patch file customweb.config

    <pipelines>
      <initialize>
        <processor type="MyProject.Website.RegisterCustomRoute, MyProject.Website" patch:before="processor[@type='Sitecore.Mvc.Pipelines.Loader.InitializeRoutes, Sitecore.Mvc']" />
      </initialize>
    </pipelines>

If you are facing the same error, make sure to add entry in “RegisterCustomRoute.cs” as highlighted above.

Tuesday, February 21, 2017

How to dynamically render WFFM Form in MVC

Yes, you can render WFFM form dynamically instead of assigning through presentation.

Below is a MVC syntax for rendering WFFM form

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new { Datasource = "<id of the form item>", UniqueId = "<unique id of the form rendering>" })

But wait a minute, what is ID {F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}”?
What to assign at Datasource and UniqueId fields in this syntax?

{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}" is the item ID of MVC Form and its located at Item Path “sitecore/layout/Renderings/Modules/Web Forms for Marketers/Mvc Form”.

Assign form item id to DataSource and UniqueId fields i.e. replace<id of the form item>” and “<unique id of the form rendering>with WFFM form id that you typically created under “sitecore/system/Modules/Web Forms for Marketers” item.

Example – Put below line in any of your cshtml file in MVC

@Html.Sitecore().Rendering("{F2CCA16D-7524-4E99-8EE0-78FF6394A3B3}", new
                        {
                            Datasource = "{C316B617-0306-454E-8729-E44F49B84CDD}",
                            UniqueId = "{C316B617-0306-454E-8729-E44F49B84CDD}"
                        });

Below is the clearer way to understand it


Sitecore WFFM Dynamic Rendering in MVC
Sitecore WFFM Dynamic Rendering in MVC


I hope you like this Sitecore tip. Stay tuned for more Sitecore related articles.

Till that happy sitecoring :)

Please leave your comments or share this article if it’s useful for you.