我一直在关注 this guide以添加帮助页面来记录我的Web API项目.我的控制器名为HelpController,我有一条路线,我试图将索引操作映射到/帮助.这是项目中唯一的MVC控制器.因为其余的是Web API控制
HelpController:
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);
// ..
}
当您从路由模板中删除api时,Web API的路由将匹配请求.如果请求与路由匹配,则不对其余路由进行进一步探测.
您可能在Global.asax中具有默认顺序,其中首先注册Web API路由,然后是MVC路由.你能分享你的Global.asax的样子吗?
编辑:
根据您的上一条评论,如果您安装HelpPage nuget包,请确保Global.asax中的订单如下所示:
AreaRegistration.RegisterAllAreas(); WebApiConfig.Register(GlobalConfiguration.Configuration); RouteConfig.RegisterRoutes(RouteTable.Routes);
