【问题标题】:How to debug MVC3/WebAPI routing 404 error?如何调试 MVC 3/Web API 路由 404 错误?
【发布时间】:2013-09-04 21:40:38
【问题描述】:

我的 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/build/models/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/build/models/x/

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

我所有的路线都可以正常工作,除了这个单一控制器上的那些,以前可以工作。我显然改变了一些东西,只是不知道是什么。

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 不,这是一个 AJAX 请求,所以我不想重定向。此外,我正在使用 NLog,我包含的 HttpException 被它捕获,并返回了 YSOD 的 HTML。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-01
  • 2011-05-28
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多