Error publishing site Asp.net mvc 4

Asked

Viewed 117 times

1

When posting the site on iis 7.5, this error occurs when trying 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 ?

Code 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 }
        );
    }

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>
}
  • This code you posted is from the view... in the controller folder, it must have a file called homeController.Cs.

2 answers

1

You must have several controllers called Home, or separated the controller into partial classes but namespaces different.

Change the name of one of the controllers, or change the namespace to stay in the same namespace.

The error you found is because the platform finds two (or more) controllers called Home. As it has no way to distinguish, returns the error.

  • I have only one controller called Home, strange this error.

  • Put the code of the controller... weird. Also put the code of your routing

  • Ready @Rsinohara.

1

This error happens when there is more than one Controller with the same name. If you use the Areas feature in MVC automatically a Home Controller is created in the new area and this can already make this error happen.

In order for the framework to be able to configure the routes correctly 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, inside the Areas/Nameparchy folder/ A Filename Filename is automatically created with the gistration.cs. Where the routes for this area are configured. 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 is the one that will solve your problem => namespaces: new string[] { "namespace do seu controller" }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.