Error publishing site asp.net mvc 4

1

When you publish the site in iis 7.5, this error occurs when you try to open the site:

  

Multiple types were found that match the controller named 'Home'. This   can happen if the route that services this request   ('{controller} / {action} / {id}') 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.

How to solve?

RouteConfig code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

Código Controller:

@{
    ViewBag.Title = "Controle de documentos";
}
@section featured {
    <section class="featured">
        <div class="content-wrapper">
            <hgroup class="title">
                <h1>@ViewBag.Title.</h1>
                <!--<h2>@*ViewBag.Message*@</h2>-->
            </hgroup>
            <p>
                Este módulo foi desenvolvido para o envio e controle de documentos do portal.
            </p>
            <p>
                @if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    @Html.ActionLink("Calendário de documentos", "Index", "CalendarioAlertaSMS");
                }
                else{
                    @Html.ActionLink("Logar", "LogOn", "Conta");
                }

            </p>
        </div>
    </section>
}
    
asked by anonymous 15.07.2015 / 15:27

2 answers

1

You should have multiple controllers called Home , or you have separated the controller into partial classes but into namespaces different.

Change the name of one of the controllers , or change the namespace so that they remain in the same namespace .

The error you encountered is because the platform encounters two (or more) controllers called Home . As it can not distinguish, it returns the error.

    
15.07.2015 / 15:45
1

This error occurs when there is more than one Controller with the same name. If you use the Areas feature in MVC you automatically create a Home Controller in the new area and this may already cause this error to happen.

In order for the framework to be able to correctly configure routes in this case, Controllers that have the same name must be in different namespaces and you must put the namespace in RouteConfig.cs as in the example below.

namespace SeuProjeto
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

             routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                namespaces: new string[] { "SeuProjeto.Controllers" }
            );
        }
    }
}

If you are using Areas within the Areas folder / CreationArea / a CreativeAreaNameAreaRegistration.cs file is automatically created. Where routes are configured for this area. It is always recommended to put the namespace in the creation of these routes as well.

namespace SeuProjeto.Areas.SuaArea
{
        public class SuaAreaAreaRegistration : AreaRegistration 
        {
            public override string AreaName 
            {
                get 
                {
                    return "SuaArea";
                }
            }

            public override void RegisterArea(AreaRegistrationContext context) 
            {
                context.MapRoute(
                    "SuaArea_default",
                    "SuaArea/{controller}/{action}/{id}",
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
                    namespaces: new string[] { "SeuProjeto.Areas.SuaArea.Controllers" }
                );
            }
        }
    }

This line will solve your problem = > namespaces: new string[] { "namespace do seu controller" }

    
15.07.2015 / 18:00