如何调试MVC 3 / Web API路由404错误?

时间:2013-09-04 21:40:38

标签: c# asp.net-mvc asp.net-mvc-3 asp.net-web-api

我的WebAPI路由定义如下:

config.Routes.MapHttpRoute(
  name: "BuildModels",
  routeTemplate: "api/build/models/{productLine}/{language}",
  defaults: new { controller = "Build", action = "GetModels" }
);

控制器:

public class BuildController : ApiController
{
    public async Task<HttpResponseMessage> GetModels(string productLine, string language)
    {
        // Omitted for brevity, I don't feel it's pertinent
        // but if you disagree I will fill this in.  
        // Nothing has changed in here and it worked before.
        return await ...;
    }
}

/ API /建造/模型/ X / EN

[HttpException]: The controller for path &#39;/api/build/models/x/en&#39; was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

/ API /建造/模型/ X /

{"Message":"The arguments value is not valid : {language}"}

我的所有路线都在工作,除了此单一控制器上的路由,之前正在工作。我显然改变了一些东西,只是无法弄清楚是什么。

1 个答案:

答案 0 :(得分:1)

您是否尝试过激活自定义错误?

<customErrors mode="On" defaultRedirect="/customerrors/500.aspx">
    <error statusCode="404" redirect="/customerrors/404.html" />
    <error statusCode="403" redirect="/customerrors/403.html" />
</customErrors>