MVC5 找到与名为“Home”的控制器匹配的多种类型

Posted

技术标签:

【中文标题】MVC5 找到与名为“Home”的控制器匹配的多种类型【英文标题】:MVC5 Multiple types were found that match the controller named 'Home' 【发布时间】:2016-03-29 02:18:28 【问题描述】:

我试图克隆一个名为 IdentitySample 的项目,但我想将其重命名为 RecreationalServicesTicketingSystem。我已经遵循了一些关于如何重命名所有内容的指南,但似乎该应用程序仍在使用 IdentitySample.Controllers.HomeController 。我尝试使用 find 函数查看代码以查看 IdentitySample 是否仍在我们的应用程序中,但我没有找到。

能否在我可能错过重命名解决方案的地方也给我一些指示?

找到了与名为“Home”的控制器匹配的多种类型。这 如果服务此请求的路由可能会发生 ('controller/action/id') 未指定要搜索的命名空间 对于与请求匹配的控制器。如果是这种情况, 通过调用“MapRoute”方法的重载来注册此路由 这需要一个“命名空间”参数。

“主页”请求找到了以下匹配的控制器: RecreationalServicesTicketingSystem.Controllers.HomeController IdentitySample.Controllers.HomeController

HomeController.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Threading.Tasks;
using System.Net;
using System.Web;
using System.Web.Mvc;
using RecreationalServicesTicketingSystem.Models;

namespace RecreationalServicesTicketingSystem.Controllers

    public class HomeController : Controller
    
        public ActionResult Index()
        
            return View();
        

        [Authorize]
        public ActionResult About()
        
            ViewBag.Message = "Your app description page.";

            return View();
        

        public ActionResult Contact()
        
            ViewBag.Message = "Your contact page.";

            return View();
        
    

查看\主页\Index.cshtml

@
    ViewBag.Title = "Home Page";


<div class="jumbotron">
    <h1>ASP.NET Identity</h1>
    <p class="lead">ASP.NET Identity is the membership system for ASP.NET apps. Following are the features of ASP.NET Identity in this sample application.</p>
    <p><a href="http://www.asp.net/identity" class="btn btn-primary btn-large">Learn more &raquo;</a></p>
</div>

<div class="row">
    <div class="col-md-4">
        <dl>
            <dt>Initialize ASP.NET Identity</dt>
            <dd>
                You can initialize ASP.NET Identity when the application starts. Since ASP.NET Identity is Entity Framework based in this sample,
                you can create DatabaseInitializer which is configured to get called each time the app starts.
                <strong>Please look in App_Start\IdentityConfig.cs</strong>
                This code shows the following
                <ul>
                    <li>When should the Initializer run and when should the database be created</li>
                    <li>Create Admin user</li>
                    <li>Create Admin role</li>
                    <li>Add Admin user to Admin role</li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Add profile data for the user</dt>
            <dd>
                <a href="http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx">Please follow this tutorial.</a>

                <ul>
                    <li>Add profile information in the Users Table</li>
                    <li>Look in Models\IdentityModels.cs for examples</li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Validation</dt>
            <dd>
                When you create a User using a username or password, the Identity system performs validation on the username and password, and the passwords are hashed before they are
                stored in the database. You can customize the validation by changing some of the properties of the validators such as Turn alphanumeric on/off, set minimum password length
                or you can write your own custom validators and register them with the UserManager.
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Register a user and login</dt>
            <dd>
                Click @Html.ActionLink("Register", "Register", "Account") and see the code in AccountController.cs and Register Action.
                Click @Html.ActionLink("Log in", "Login", "Account") and see the code in AccountController.cs and Login Action.
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Social Logins</dt>
            <dd>
                You can the support so that users can login using their Facebook, Google, Twitter, Microsoft Account and more.
            </dd>
            <dd>
                <ul>
                    <li>
                        <a href="http://www.windowsazure.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/">Add Social Logins</a>
                    </li>
                    <li>
                        <a href="http://blogs.msdn.com/b/webdev/archive/2013/10/16/get-more-information-from-social-providers-used-in-the-vs-2013-project-templates.aspx">Get more data about the user when they login suing Facebook</a>
                    </li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Basic User Management</dt>
            <dd>
                Do Create, Update, List and Delete Users.
                Assign a Role to a User.
                Only Users In Role Admin can access this page. This uses the [Authorize(Roles = "Admin")] on the UserAdmin controller.
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Basic Role Management</dt>
            <dd>
                Do Create, Update, List and Delete Roles.
                Only Users In Role Admin can access this page. This authorization is doen by using the [Authorize(Roles = "Admin")] on the RolesAdmin controller.
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Account Confirmation</dt>
            <dd>
                When you register a new account, you will be sent an email confirmation.
                You can use an email service such as <a href="http://www.windowsazure.com/en-us/documentation/articles/sendgrid-dotnet-how-to-send-email/">SendGrid</a> which integrate nicely with Windows Azure and requires no configuration or
                set up an SMTP server to send email.
                You can send email using the EmailService which is registered in App_Start\IdentityConfig.cs
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Two-Factor Authentication</dt>
            <dd>
                This sample shows how you can use Two-Factor authentication. This sample has a SMS and email service registered where you can send SMS or email for sending the security code.
                You can add more two-factor authentication factors such as QR codes and plug them into ASP.NET Identity.
                <ul>
                    <li>
                        You can use a SMS using <a href="https://www.twilio.com/">Twilio</a> or use any means of sending SMS. Please <a href="https://www.twilio.com/docs/quickstart/csharp/sms/sending-via-rest">read</a> for more details on using Twilio.
                        You can send SMS using the SmsService which is registered in App_Start\IdentityConfig.cs
                    </li>
                    <li>
                        You can use an email service such as <a href="http://www.windowsazure.com/en-us/documentation/articles/sendgrid-dotnet-how-to-send-email/">SendGrid</a> or
                        set up an SMTP server to send email.
                        You can send email using the EmailService which is registered in App_Start\IdentityConfig.cs
                    </li>

                    <li>
                        When you login, you can add a phone number by clicking the Manage page.
                    </li>
                    <li>
                        Once you add a phone number and have the Phone service hooked to send a SMS, you will get a code through SMS to confirm your phone number.
                    </li>
                    <li>
                        In the Manage page, you can turn on Two-Factor authentication.
                    </li>
                    <li>
                        When you logout and login, after you enter the username and password, you will get an option of how to get the security code to use for two-factor authentication.
                    </li>
                    <li>
                        You can copy the code from your SMS or email and enter in the form to login.
                    </li>
                    <li>
                        The sample also shows how to protect against Brute force attacks against two-factor codes. When you enter a code incorrectly for 5 times then you will be
                        lockedout for 5 min before you can enter a new code. These settings can be configured in App_Start\IdentityConfig.cs by setting  DefaultAccountLockoutTimeSpan and MaxFailedAccessAttemptsBeforeLockout on the UserManager.
                    </li>
                    <li>
                        If the machine you are browsing this website is your own machine, you can choose to check the "Remember Me" option after you enter the code.
                        This option will remember you forever on this machine and will not ask you for the two-factor authentication, the next time when you login to the website.
                        You can change your "Remember Me" settings for two-factor authentication in the Manage page.
                    </li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Account Lockout</dt>
            <dd>
                Provide a way to Lockout out the user if the user enters their password or two-factor codes incorrectly.
                The number of invalid attempts and the timespan for the users are locked out can be configured.
                A developer can optionally turn off Account Lockout for certain user accounts should they need to.
            </dd>
            <ul>
                <li>Account LockOut settings can be configured in the UserManager in IdentityConfig.cs</li>
            </ul>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Security Token provider</dt>
            <dd>
                Support a way to regenerate the Security Token for the user in cases when the User changes there password or any other security related information such as removing an associated login(such as Facebook, Google, Microsoft Account etc).
                This is needed to ensure that any tokens generated with the old password are invalidated. In the sample project, if you change the users password then a new token is generated for the user and any previous tokens are invalidated.
                This feature provides an extra layer of security to your application since when you change your password, you will be logged out from everywhere (all other browsers) where you have logged into this application.
            </dd>
            <dd>
                <ul>
                    <li>The provider is registered when you add CookieAuthentication in StartupAuth to your application.</li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Password Reset</dt>
            <dd>
                Allows the user to reset their passwords if they have forgotten their password. In this sample users need to confirm their email before they can reset their passwords.
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Custom Storage providers</dt>
            <dd>
                You can extend ASP.NET Identity to write your own custom storage provider for storing the ASP.NET Identity system and user data
                in a persistance system of your choice such as MondoDb, RavenDb, Azure Table Storage etc.
            </dd>
            <dd>
                <ul>
                    <li>
                        <a href="http://www.asp.net/identity/overview/extensibility/overview-of-custom-storage-providers-for-aspnet-identity">
                            learn more on how to implement your own storage provider
                        </a>
                    </li>
                </ul>
            </dd>
        </dl>
    </div>
    <div class="col-md-4">
        <dl>
            <dt>Documentation</dt>
            <dd>
                <ul>
                    <li>
                        Tutorials: <a href="www.asp.net/identity">www.asp.net/identity</a>
                    </li>
                    <li>
                        ***: <a href="http://***.com/questions/tagged/asp.net-identity">http://***.com/questions/tagged/asp.net-identity</a>
                    </li>
                    <li>
                        Twitter: #identity #aspnet
                    </li>
                    <li>
                        <a href="http://curah.microsoft.com/55636/aspnet-identity">ASP.NET Identity on curah</a>
                    </li>
                    <li>
                        Have bugs or suggestions for ASP.NET Identity <a href="http://aspnetidentity.codeplex.com/">http://aspnetidentity.codeplex.com/</a>
                    </li>
                </ul>
            </dd>
        </dl>
    </div>
</div>

【问题讨论】:

这是一个非常具有描述性的错误信息。 查看 Darin Dimitrov 的解决方案:***.com/questions/7842293/… @Hendry 我知道为什么会这样,但不知道解决方案。 因为你重命名了你的主二进制文件,你可能在bin文件夹中还有旧的。执行“清理”,然后检查bin 文件夹并确保它为空。如果之后不为空,删除binobj文件夹,然后构建,应该可以了。 @MichaelCoxon 感谢您的建议,但 Nico 的方法效果很好。 【参考方案1】:

错误是放弃答案,基本上你有多个名为HomeController 的控制器。我假设你没有删除原来的IdentitySample.Controllers.HomeController

你有两个选择。

    删除IdentitySample.Controllers.HomeController 实例。 更改您的路线,使您的路线包含要搜索的命名空间(如错误中所列)。

如果您想使用选项 #2,请在您的路由表中更改默认路由

routes.MapRoute(
    "Default", 
    "controller/action/id", 
    new  controller = "Home", action = "Index", id = UrlParameter.Optional ,
);

routes.MapRoute(
    "Default", 
    "controller/action/id", 
    new  controller = "Home", action = "Index", id = UrlParameter.Optional ,
    namespaces: new[]  "RecreationalServicesTicketingSystem.Controllers" 
);

在 App_Start\RouteConfig.cs 中

第二个例子告诉我们在"RecreationalServicesTicketingSystem.Controllers"命名空间中寻找控制器。

【讨论】:

只是添加。应将命名空间添加到所有路由。【参考方案2】:

我遇到了同样的问题,发现旧版本在“bin”文件夹中创建了编译文件。

一旦我删除了这些,错误就消失了。

【讨论】:

非常感谢!我什至做了一个硬 git reset,但解决方案就是这么简单 我在我的解决方案中添加了另一个项目,该项目有冲突的名称空间,在我删除它之后我仍然遇到这个问题。删除文件夹 bin 和 obj 让我重新启动并运行。【参考方案3】:

当 bin 文件夹中存在 2 个具有相同名称空间但名称不同的 dll 时,我遇到了问题,刚刚删除了不需要的 dll,问题已得到解决。

【讨论】:

【参考方案4】:

就我而言,此处建议的解决方案无效。发现AppStart中RouteConfig中的namespace已经改成了IdentitySample;所以我只是将我在 MapRoute 中设置的命名空间更改为这个新名称。

【讨论】:

以上是关于MVC5 找到与名为“Home”的控制器匹配的多种类型的主要内容,如果未能解决你的问题,请参考以下文章

找到了与名为“Home”的控制器匹配的多种类型。 (奇怪的错误)

找到了与名为“Home”的控制器匹配的多种类型 - 在两个不同的区域中

找到多个与名为“Home”的控制器匹配的类型

找到多个与名为“Home”的控制器匹配的类型

找到多个与名为“Home”的控制器匹配的类型

找到多个与名为“Home”的控制器匹配的类型。解决方法