自托管ASP.NET Web API时找不到404,在IIS中运行正常

时间:2016-09-26 09:04:48

标签: c# asp.net .net rest asp.net-web-api

我目前仍然使用自我托管的Web API,我试图在单元测试中使用。

无论我尝试什么路线,我都会收到404找不到的错误。问题是,当在IIS中托管相同的Web API时,一切正常。我猜测它在我的测试配置中是一个简单的错字/错误,但是我无法使它工作,任何帮助都会受到赞赏。

以下是我在IIS中托管时用于Web API的完全正常工作的配置

var config = new HttpSelfHostConfiguration(webApiURL);

#if DEBUG
    // For debug purposes
    config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
#endif

// Web API routes
config.MapHttpAttributeRoutes();

// Only supports JSON formatter
config.Formatters.Clear();
config.Formatters.Add(new JsonMediaTypeFormatter());
config.Formatters.JsonFormatter.SerializerSettings = CustomJsonSerializerSettings.Create();

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

// Custom filter
var providers = config.Services.GetFilterProviders().ToList();
var defaultprovider = providers.Single(i => i is ActionDescriptorFilterProvider);
config.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);
config.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider), new UnityFilterProvider(this.UnityContainer));

// Overwrites the IoC configuration
config.DependencyResolver = new UnityDependencyResolver(this.UnityContainer);

FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

this.webApiServer = new HttpSelfHostServer(config);
this.webApiServer.OpenAsync().Wait();
enter code here

这是我用于单元测试的非工作配置

public class Startup
{
    private static IUnityContainer container;

    /// <summary>
    /// Overwrites the IoC configuration.
    /// </summary>
    /// <param name="container">The container.</param>
    public static void ConfigureIoC(IUnityContainer container)
    {
        Startup.container = container;
    }

    /// <summary>
    /// This code configures Web API. The Startup class is specified as a type
    /// parameter in the WebApp.Start method.
    /// </summary>
    /// <param name="appBuilder">The application builder.</param>
    public void Configuration(IAppBuilder appBuilder)
    {
        if (Startup.container == null)
        {
            throw new Exception("Call ConfigureIoC first");
        }

        // Configure Web API for self-host.
        HttpConfiguration config = new HttpConfiguration();
        config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{action}/{id}",
              defaults: new { id = RouteParameter.Optional });

        // Web API routes
        config.MapHttpAttributeRoutes();

        // Only supports JSON formatter
        config.Formatters.Clear();
        config.Formatters.Add(new JsonMediaTypeFormatter());
        config.Formatters.JsonFormatter.SerializerSettings = CustomJsonSerializerSettings.Create();

        // Custom filter
        var providers = config.Services.GetFilterProviders().ToList();
        var defaultprovider = providers.Single(i => i is ActionDescriptorFilterProvider);
        config.Services.Remove(typeof(System.Web.Http.Filters.IFilterProvider), defaultprovider);
        config.Services.Add(typeof(System.Web.Http.Filters.IFilterProvider), new UnityFilterProvider(container));

        // Overwrites the IoC configuration
        config.DependencyResolver = new UnityDependencyResolver(Startup.container);

        appBuilder.UseWebApi(config);
    }
}

我也尝试了新的Owin托管服务,结果相同:

"aggregations" : {
                "startcount" : {
                    "terms" : {
                        "script" : "round(doc['startat'].value/1000)",
                        "size" : 1000,
                        "order" : { "_term" : "asc" }
                    }
                }
             }

我从Postman开始的REST请求在IIS中托管时是成功的,而在我的单元测试中作为自托管服务托管时是404。

任何提示?

1 个答案:

答案 0 :(得分:1)

您应该检查自托管应用程序是否包含虚拟目录(如IIS)。正如您所提到的,确保基本路径有效。