web.api中的处理程序路由

时间:2019-03-29 10:59:36

标签: asp.net

我有web.api 2项目。我还尝试向其中添加处理程序。但是我发送的每个请求(http://api.xxxx.xxx/handler)都返回404代码错误。我意识到问题在于路由配置,但是我该如何解决?

web.config

<system.webServer>
  <validation validateIntegratedModeConfiguration="false" />
  <modules runAllManagedModulesForAllRequests="true">
    <remove name="WebDAVModule" />
    <!-- ADD THIS -->
  </modules>  
    <handlers accessPolicy="Read, Execute, Script">
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <remove name="OPTIONSVerbHandler" />
      <remove name="TRACEVerbHandler" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
      <add name="ChatHandler" verb="*" path="/handler/" type="ProjectAPI.Handler.ChatHandler" />
    </handlers>
  </system.webServer>

Global.asax.cs

public class WebApiApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");
        RouteTable.Routes.Ignore("handler/{*path}");

        GlobalConfiguration.Configure(WebApiConfig.Register);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您没有更改WebApiConfig路由,则默认URL应为: http://api.xxxx.xxx/api/handler。我从未见过需要将其添加到配置中,这是为什么呢?仅将WebApiConfig.Register中的配置从〜/ api /更改为〜/,它应该可以工作。发布更多代码,因为这就像“从茶叶中读取”:)

相关问题