bmherold
bmherold

Reputation: 321

WCF + MVC + WebAPI handlers and routing

I recently upgraded Visual Studio to 2013 which now only uses IIS Express by default (instead of Cassini) and found I could no longer hit my WCF service javascript endpoints: Myservice.svc/js or /jsdebug, but hitting the .svc directly would work fine.

I've found that if I remove the <handlers> section of my web.config (which includes some ExtensionlessUrlHandler items), the functionality to the script endpoints is restored. I'm curious what repercussions removing the following items would have, and if there is an alternative solution.

Here is the block I removed from my Web.config:

<handlers>
  <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" />
</handlers>

Some context, I'm using .NET 4.5, MVC 5.1, WebAPI v2.1 and WCF all in one project. Essentially the newest versions of all the components.

Update 1: I'm also noticing that any WebAPI PUT's are coming back as a 404 with these handlers removed; so something is obviously missing. The other minor epiphany, is that when deployed to a live server (IIS 7.5 [Version 7.5.7600.16385]) everything works great. So there has to be a difference in the routing or handler mappings of the Visual Studio 2013 integrated version of IIS Express and a full IIS 7.5 setup.

ALSO, If I create a brand new ASP.NET web app and add a WCF service, everything works fine locally. If I then add in a WebAPI controller, the handlers section below is added to the web.config and breaks all the WCF calls. This makes it sound more like a 'bug' in Microsoft's default configuration when MVC, WCF, and WebAPI are all a part of one application.

Update 2: Here's a basic test case anyone can follow to reproduce the error I'm having.

Process to recreate base issue: (WebAPI working, WCF *.svc/js results in 404)

  1. New Project -> Asp.NET Web Application, using Web API Template (includes MVC and Web API references)
  2. Add new "WCF Service (ajax-enabled) to project root named "TestService"
  3. Start project.
  4. Verify that /help route works for MVC (renders help page)
  5. Verify that GET /api/values/1 works for WebApi (returns value)
  6. * Verify that PUT /api/values/1 works for WebApi (returns nothing - void method) *
  7. Verify TestService.svc can be hit for WCF (shows metadata page)
  8. * Verify that TestService.svc/js FAILS with a 404 for WCF - Routing issue! *

Partial Fix: (WebAPI PUT broken, WCF can render JS request)

  1. Continue from above...
  2. Update Web.config and comment out <handlers> section
  3. Start project.
  4. Verify that /help route works for MVC (renders help page)
  5. Verify that GET /api/values/1 works for WebApi (returns value)
  6. * Verify that PUT /api/values/1 FAILS with a 404 for WebApi - Routing issue! *
  7. Verify TestService.svc can be hit for WCF (shows metadata page)
  8. * Verify that TestService.svc/js renders necessary JavaScript for WCF *

Upvotes: 3

Views: 2922

Answers (1)

Kenneth Ito
Kenneth Ito

Reputation: 5261

This is actually a mvc routing issue. Please modify your ~/App_Start/RouteConfig.cs as below. You need to keep the ExtensionlessUrlHandler in order for mvc routing to function normally on extensionless urls.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //This line is the addition.
        routes.IgnoreRoute("{resource}.svc/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Upvotes: 3

Related Questions