WebAPI 2.2上的Elmah.axd - 未找到HTTP资源

时间:2015-06-22 18:41:06

标签: asp.net-web-api asp.net-web-api2 elmah asp.net-web-api-routing

我试图在我的broswer中访问/elmah.axd,但它返回:

{"message":"No HTTP resource was found that matches the request URI 'http://services.domain.com/elmah.axd'."}

服务器是本地的(127.0.0.1),即便如此我也有我的web.config Elmah设置以这种方式保护它:

<elmah>
    <security allowRemoteAccess="true" />
</elmah>

我的WebApiConfig看起来像:

public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Locally only you will be able to see the exception errors
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;

            // Web API routes
            config.Routes.IgnoreRoute("elmah", "elmah.axd");
            config.Routes.IgnoreRoute("allemah", "elmah.axd/{*pathInfo}");
            config.Routes.IgnoreRoute("elmahgeneric", "{resource}.axd/{*everything}");
            config.MapHttpAttributeRoutes();

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Remove the XML formatter
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }

我甚至试图在3种组合中只忽略一条路线而没有运气。

最后我的global.asax看起来像这样:

protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

        }

任何我可能遗失的暗示或想法都会很好。

提前致谢,非常感谢您花时间研究这个。

4 个答案:

答案 0 :(得分:9)

我终于成功了。

添加到WebApiConfig.cs文件

config.Routes.MapHttpRoute("AXD", "{resource}.axd/{*pathInfo}", null, null, new StopRoutingHandler());

WebApiConfig.cs文件的整个代码如下所示:

public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            // Locally only you will be able to see the exception errors
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.LocalOnly;

            // Web API routes
            config.MapHttpAttributeRoutes();

            var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
            jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // Remove the XML formatter
            config.Formatters.Remove(config.Formatters.XmlFormatter);

            config.Routes.MapHttpRoute("AXD", "{resource}.axd/{*pathInfo}", null, null, new StopRoutingHandler());

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }


    }

最后的更改是将它添加到application_start方法

下的global.asax中
RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");

特别感谢所有帮助我解决这个问题的人。

答案 1 :(得分:3)

When I added the following in in the global.asax under the application_start method:

RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");

It did not work until I put it above ...

GlobalConfiguration.Configure(WebApiConfig.Register);

The order of the routes that you add to the Routes property is significant, because the application uses the first route in the collection that matches the URL.

https://msdn.microsoft.com/en-us/library/system.web.routing.routetable.routes(v=vs.110).aspx

答案 2 :(得分:2)

最可能的问题是缺少处理程序来生成错误页面。

确保为web.config文件配置了以下处理程序:

<system.web>
    ...
    <httpHandlers>
      ...
      <add verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </httpHandlers>


<system.webServer>
    ...
    <handlers>
      ...
      <add name="Elmah" verb="POST,GET,HEAD" path="elmah.axd" type="Elmah.ErrorLogPageFactory, Elmah" />
    </handlers>

使用这些设置后,您应该能够访问elmah.axd并按预期保护它。

您可以在此处获取从新项目更新的示例配置文件:https://brutaldev.com/download/30987439.zip

答案 3 :(得分:2)

我添加

config.Routes.IgnoreRoute("axd", "{resource}.axd/{*pathInfo}");

WebApiConfig就足够了。