asp.net-mvc - 未找到与名为 'User' 的 Controller 匹配的类型

标签 asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing asp.net-web-api-routing

我正在尝试导航到其 URL 采用以下格式的页面: 本地主机:xxxxx/User/{id}/VerifyEmail?secretKey=xxxxxxxxxxxxxxx

我在 RouteConfig.cs 文件中添加了一条新路由,因此我的 RouteConfig.cs 如下所示:

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

        routes.MapRoute(
            name: "VerifyEmail",
            url: "User/{id}/VerifyEmail",
            defaults: new { controller = "User", action = "VerifyEmail" }
        );

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

不幸的是,当尝试导航到该 URL 时,我得到了这个页面:

<Error>
    <Message>
        No HTTP resource was found that matches the request URI 'http://localhost:52684/User/f2acc4d0-2e03-4d72-99b6-9b9b85bd661a/VerifyEmail?secretKey=e9bf3924-681c-4afc-a8b0-3fd58eba93fe'.
    </Message>
    <MessageDetail>
        No type was found that matches the controller named 'User'.
    </MessageDetail>
</Error>

这是我的 UserController:

public class UserController : Controller
{

    // GET      /User/{id}/VerifyEmail
    [HttpGet]
    public ActionResult VerifyEmail(string id, string secretKey)
    {
        try
        {
            User user = UsersBL.Instance.Verify(id, secretKey);
            //logger.Debug(String.Format("User %s just signed-in in by email.",
                user.DebugDescription()));
        }
        catch (Exception e)
        {
            throw new Exception("Failed", e);
        }
        return View();
    }
}

请告诉我我做错了什么?

最佳答案

就我而言, Controller 定义为:

    public class DocumentAPI : ApiController
    {
    }

将其更改为以下内容有效!

    public class DocumentAPIController : ApiController
    {
    }

类名必须以Controller结尾!

编辑:正如 @Corey Alix 所建议的,请确保 Controller 具有 public 访问修饰符;非公共(public) Controller 会被路由处理程序忽略!

关于asp.net-mvc - 未找到与名为 'User' 的 Controller 匹配的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17811142/

相关文章:

javascript - 浏览器后退按钮不起作用

c# - Asp.net MCV4 框架问题

c# - 为什么程序集似乎被引用但实际上没有

asp.net-mvc - 自定义授权属性

c# - .net core 2.0 mvc + kestrel - 如何将目录锁定到 Controller 操作?

c# - 用 twitter bootstrap 装饰 @Html.Textbox?

c# - 如何在 MVC 路由中启用特殊字符?

jquery - Restful Webservices 比较 WebAPI 与 MVC

c# - 如何将数据写入 ASP.NET MVC 中的 App_Data 文件夹?

c# - 在 Web API 的 Swagger 文档中支持基于 token 的身份验证