【发布时间】:2013-12-21 03:25:59
【问题描述】:
我一直关注this guide 添加帮助页面来记录我的 Web API 项目。我的控制器被命名为 HelpController,我有一个路由,我试图用它来将 Index 操作映射到 /Help。这是项目中唯一的 MVC 控制器。因为其余的是 Web API 控制器,所以我们从 WebAPIConfig.cs 中的默认路由中删除了“/api”前缀。
帮助控制器:
public class HelpController : Controller
{
public ActionResult Index()
{
var apiExplorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
return View(apiExplorer);
}
}
和路由配置:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "help",
defaults: new { controller = "Help", action = "Index"});
}
}
在 Global.asax.cs 中
protected void Application_Start()
{
// ..
WebApiConfig.Register(GlobalConfiguration.Configuration);
RouteConfig.RegisterRoutes(RouteTable.Routes);
// ..
}
但是当我尝试在浏览器中导航到 /help 时,我收到以下错误消息。
<Error>
<Message>No HTTP resource was found that matches the request URI 'http://localhost/ws/help'.</Message>
<MessageDetail>No type was found that matches the controller named 'help'.</MessageDetail>
</Error>
编辑:该消息包含 /ws/help,因为应用程序托管在 IIS 中的 localhost/ws 上。
有谁知道是什么原因导致 ASP.NET 找不到我的 HelpController?
更新:如果我在 Application_Start 中更改 RouteConfig 和 WebApiConfig 注册调用的顺序,我会得到 404。
protected void Application_Start()
{
// ..
RouteConfig.RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
// ..
}
【问题讨论】:
-
这条路线似乎适用于
http://localhost/help,您似乎要去http://localhost/ws/help,对吗? -
对不起,是的。这在 IIS 的网站中托管为一个名为 ws 的应用程序。
-
错误信息指的是
help而不是Help(注意大小写)你是否使用Help作为url进行了测试。 -
@TomStyles 是的。不幸的是,同样的反应。
标签: asp.net asp.net-mvc asp.net-web-api routing