6

I would like to use ASP.NET WebApi inside a SharePoint 2013 farm solution.

I know it is not supported out-of-the-box, but I found SignalR can be run by means of a simple HttpModule, so I was wondering whether a similar appoach could be used.

Thanks in advance, Rich

UPDATE June 2013

Made it working by reworking the HTTP Module shown in the mentioned post:

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "kms2013/api/{controller}/{action}",
                defaults: new { }
            );
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
config.Services.Replace(typeof(IAssembliesResolver), new SPAssemblyResolver());

HostingEnvironment.RegisterVirtualPathProvider(new WebAPIVirtualPathProvider());

SPAssemblyResolver

public class SPAssemblyResolver : IAssembliesResolver
{
    public ICollection<Assembly> GetAssemblies()
    {
        return new List<Assembly> { Assembly.GetExecutingAssembly() };
    }
}

WebAPIVirtualPathProvider

Same as SignalRVirtualPathProvider shown in the post.

NEW ISSUE

The only problem with this approach is ScriptResource.axd and WebResource.axd now break when SP references them in a page. I tried to add an ignore route:

RouteTable.Routes.Add(new Route("{resource}.axd", new StopRoutingHandler()));

But I keep getting 401 Unauthorized. Removing the module clears the error, so I guess we're still lacking one last piece of the puzzle.

user2363245
  • 1,675
  • 2
  • 11
  • 13
  • I assume you have to adjust the Virtual Path Provider, see an example here: https://spsignalr.codeplex.com/SourceControl/latest#MaxMelcher.SPSignalR/SignalRVirtualPathProvider.cs – Max Melcher Jun 27 '13 at 10:46
  • 1
    Fine, the VPP implementation you suggested traps all the missing cases and everything's working now. Previously, _DirectoryExists_ yield various kind of errors when lacking _virtualDir.Contains("my/api/path")_, ranging from SharePoint JS errors, broken search, etc.. – user2363245 Jul 02 '13 at 10:07
  • I'm trying to do the same and I'm almost there, but I get an error because it can't find the Controller. Deploying only the webapi project it works fine, but inside SP it breaks. Do you have any suggestion? – Maurizio Pozzobon Dec 15 '14 at 12:03

1 Answers1

1

Yes, the same approach should work.

Create a web api project and check the app init part - then follow my blog post you referenced already.

Btw: Ask at sharepoint.stackexchange.com - maybe someone has a better solution.

Max Melcher
  • 200
  • 1
  • 12