AttributeRouting icon indicating copy to clipboard operation
AttributeRouting copied to clipboard

Web API WebHost Workarounds

Open mccalltd opened this issue 13 years ago • 7 comments

  • Features Not Working
  • Routes Not Working in RC+
  • ApiExplorer Shows Extraneous Routes

This is going to be a master list of workarounds for Web API integration issues. Unfortunately, the fixes to most problems won't be available until vNext of Web API, as they require changing the accessibility of some framework objects that likely won't make it in Web API RTM (but will make it into future versions as Microsoft wants to ensure compatibility with this project).

Please add your items here if you don't see them. I'll keep this first comment up-to-date with all the problems and their current state of resolution.

Features Not Working

There's an internal object in the web host bits that make it impossible to support the following features:

  • performance enhancements when matching routes,
  • custom route handlers,
  • querystring parameter constraints,
  • subdomain routing,
  • localization url generation and constraints,
  • lowercasing, appending prefixes, etc to generated routes.

Routes Not Working in RC+ (#83)

Web API RC sealed a vital interface for route detection by the underlying framework. Though the interface is now public, the change won't be released until vNext. So here are some workarounds:

  • Use AR attributes in combination with HttpGet, HttpPost, HttpPut, or HttpDelete attributes from System.Web.Http:
[GET("some/url"), HttpGet]
public string Method1() {}

[PUT("some/url"), HttpPut]
public string Method2() {}

[POST("some/url"), HttpPost]
public string Method3() {}

[DELETE("some/url"), HttpDelete]
public string Method4() {}
  • Use AR attribute in combination with method names that start with the verb:
[GET("some/url")]
public string GetMethod() {}

[POST("some/url")]
public string PostMethod() {}

[PUT("some/url")]
public string PutMethod() {}

[DELETE("some/url")]
public string DeleteMethod() {}

ApiExplorer Shows Funky Extraneous Routes (#102)

Nothing can be done at this time to resolve this. The workaround until Web API vNext is to not have two actions with the same name (see scenario in issue).

Changes Needed to ASP.NET Web Stack

  • internal System.Web.Http..Controllers.IActionHttpMthodProvider needs to be public (http://aspnetwebstack.codeplex.com/workitem/240).
  • internal System.Web.Http.WebHost.Routing.HttpWebRoute needs to be public (https://aspnetwebstack.codeplex.com/workitem/305).

mccalltd avatar Jul 30 '12 22:07 mccalltd

Workaround for #102: due to two routes pointing to actions with the same name, which trips up the internals of the APiExplorer. The default web api routing mechanism doesn't have this problem because only one generic route handles both actions. In AR, you get two.

mccalltd avatar Aug 05 '12 21:08 mccalltd

Resolved #86, I believe.

mccalltd avatar Aug 05 '12 22:08 mccalltd

Note that #122 will resolve #112 and #117 in one bang. Will also make http://aspnetwebstack.codeplex.com/workitem/305 irrelevant.

mccalltd avatar Sep 05 '12 07:09 mccalltd

Resolved in v3: Irrelevant
  • internal System.Web.Http.WebHost.Routing.HostedHttpRoute and HttpWebRoute need to be public (http://aspnetwebstack.codeplex.com/workitem/305). The work item says disregard, but if both of these objects were public (and HttpWebRoute was public in the RC), then I could simply mimic the behavior of the framework's MapHttpRoute method.

mccalltd avatar Sep 07 '12 06:09 mccalltd

RESOLVED in v3: Custom DelegatingHandler Not Called (#112 & #117)

Under the covers, for ASP.NET Web API, AR stuffs a short-circuiting message handler into the GlobalConfiguration in order to circumvent the framework bug discussed in #90. If you have your own custom handler that you need to register, please do so before calling the MapHttpAttributeRoutes extensions method. This will involve taking the code dropped into your project in App_Start/AttributeRoutingHttp.cs and moving it into RouteConfig or elsewhere. The App_Start stuff is run before your global.asax Application_Start method. So you need to defer a bit to give yourself time to register your custom handlers first.

mccalltd avatar Sep 07 '12 06:09 mccalltd

Could you consider adding the ObsoleteAttribute with an appropriate warning to the functionality that is known to cause problems currently?

I recently ran into issue #83 while I was in the middle of development. It took me quite some time to find this.

michaeljbailey avatar Jan 14 '13 16:01 michaeljbailey

All of these issues are resolvable using the latest nightly builds of Web API. But still have to wait for releases....

mccalltd avatar Feb 19 '13 21:02 mccalltd