3
votes

I'm new to Sitecore MVC and currently with web forms I have all the sites organized under:

\Website\Sites\Site1\css|js|Layouts|Sublayouts|etc.
\Website\Sites\Site{n}\css|js|Layouts|Sublayouts|etc.

I'm able to add an MVC site to my solution and works fine alongside the web forms sites; however, adding a second MVC site that happen to have the same controller/view names generates a conflict.

For example, if I create a controller for Site1

Controllers/Site1/FooController (has index and hello)

Then the views are:

Views/Foo/Index
Views/Foo/Hello

But if Site2 also has a controller with the same name then it's a conflict:

Controllers/Site2/FooController (has index and hello)

Then the views are:

Views/Foo/Index
Views/Foo/Hello

But they're used by Site1.

The question is how to setup two (or more) MVC sites that happen to have the same controller/view names. Is there a recommended way to structure the sites in the solution or do I have to override pipelines/processors?

Thanks

Update:

Thanks everyone. Areas solved my problem but introduced two new problems:

  1. The conflict in the controller names which solved by putting the namespace, class and dll names in the controller name in Sitecore - reference: http://blog.xcentium.com/2014/03/sitecore-mvc-and-duplicate-controller-names/

  2. When the controller returns a view, I have to put the full path of the view; otherwise, I get an error where the view is not found. For example: return View("~/Areas/Site1/Views/Home/Index.cshtml");

I'm looking into a fix provided from a developer from Sitecore's forum: http://www.chrisvandesteeg.nl/2014/06/13/sitecore-mvc-in-a-multisite-environment-areas/

I'll try it out and report back.

4

4 Answers

1
votes

We had the similar problem and answer was to separate out every site with MVC areas and they works perfectly. Though we ran into issue of controller name duplication but that can be resolved by adding the namespace during the area route registration.

But a clean way to implement this is to let Sitecore know about the MVC areas and initialize your controller/action with area and namespace. This process has been blogged by Kevin and he has a package as well. It expect you to define the area name in controller rendering.

http://webcmd.wordpress.com/2013/01/24/sitecore-mvc-area-controller-rendering-type/

To avoid the hard coded path of view(s) you can always extend controller rendering template to add view path and create an action filter to add the view path after action is executed. Add the below code in action filter and register the filter in sitecore action filter registration pipeline.

public void OnActionExecuted(ActionExecutedContext filterContext)
{ 

     ViewResult result= filterContext.Result as ViewResult;
     if(result == null) return;

     Rendering redering = RenderingContext.CurrentOrNull.With(x=>x.Rendering).Return(x=>x,null);

     string viewName= rendering.Return(r=> r.GetFieldValue(CustomMvcSettings.ViewPathField), string.Empty);
     if(String.IsnullOrEmpty(viewName)) return;

     result.ViewName = viewName;

}
0
votes

The best thing you can do is split your websites up in different projects in the same solution.
Building two websites in the same project can become unstructured and messy.

I like to structure it like this

After that you can route the controllers with the same name using the different namespaces.

Sitecore mvc duplicate controller

0
votes

Just to keep this topic in sync with the SDN forum,

I recommend using a sitecore specific constraint, as described at http://www.chrisvandesteeg.nl/2014/06/13/sitecore-mvc-in-a-multisite-environment-areas/

This solution allows you to set the attribute mvcArea on your configuration node