Adding a WCF service to an ASP.NET MVC project, just choose Add new item and choose the WCF Service item. However when you try to connect to your WCF service(e.g. http://localhost:8000/sampleservice.svc), you’ll end up with the following exception:
“Resource not found”
The WCF service URL interferes with the default MVC routing so instead of loading up the WCF service, the ASP.NET MVC framework tries to find a matching controller and action.
To fix this, extend the routing with 2 extra ignores:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void RegisterRoutes(RouteCollection routes) | |
{ | |
routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); | |
//Extra ignores to support WCF in ASP.NET MVC | |
routes.IgnoreRoute("{resource}.svc/{*pathInfo}"); | |
routes.IgnoreRoute("{resource}.svc"); | |
routes.MapRoute( | |
name: "Default", | |
url: "{controller}/{action}/{id}", | |
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } | |
); | |
} |