【发布时间】:2013-07-22 13:53:52
【问题描述】:
我正在尝试导航到其 URL 采用以下格式的页面: localhost: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>
这是我的用户控制器:
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();
}
}
请告诉我我做错了什么?
【问题讨论】:
-
奇怪的是错误以XML文档的形式返回。就好像您使用的是劫持这条路线的 ASP.NET Web API。你在使用 ASP.NET Web API 吗?如果是,它的路由定义是怎样的?
-
是的,我使用的是 ASP.NET Web API,你可以看到上面的路由定义
-
不,等一下。您似乎混淆了 ASP.NET Web API 和 ASP.NET MVC。这是两种完全不同的技术。您在问题中显示的是 ASP.NET MVC 路由定义和控制器。 ASP.NET Web API 使用完全不同的堆栈。 ASP.NET Web API 控制器从
ApiController派生而不是从Controller派生,并且不会返回与您的问题中显示的相反的 ActionResults。 -
我知道。当你创建一个 WEB API 应用程序时,你会得到 HomeController,它继承自 Controller 而不是 ApiController。我的问题不在于我的 WEB API 控制器。我让他们都能正常工作。问题是,除了我可以访问的 HomeController 中的 Index 方法之外,所有其他方法都无法识别,并且出现异常“未找到与名为 'User' 的控制器匹配的类型”
标签: asp.net-mvc asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing asp.net-web-api-routing