asp.net mvc 4 - Running WebApi alongside MVC project in Azure -


i have solution mvc project , separate web api project. have been working on the mvc portion , have had hosted web role in cloud service project.

now need build on web api. running solution in azure emulator i'm unable hit web api controller, 404 not found.

what steps need take after creating webapi project in solution in order enable mvc project alongside hit api controller using jquery ajax call (from mvc project)?

note: portion of application consuming api in subdomain (ex "consumingapp.myapplication.com/api/values". don't know if makes difference.

ok here steps run mvc , webapi side side in azure under single web role:

  1. add mvc project web role in cloud service project.
  2. open servicedefinition.csdef configure follows:

    <sites>    <site name="web" physicaldirectory="..\..\..\application.web">       <bindings>          <binding name="endpoint1" endpointname="endpoint1" />       </bindings>    </site>    <site name="web.api" physicaldirectory="..\..\..\application.web.api">       <bindings>          <binding name="endpoint1" endpointname="endpoint1" hostheader="api.myapplication.com"/>       </bindings>    </site>       </sites> 

    note: physical directory relative definition file deployed cloudproject\bin\release need track 3 nodes solution folder. had trouble trying run project automatically switch physicaldirectory bad path. fix right clicked cloud project > project dependencies > make sure both projects checked off. make sure save , close instances of text editor definition file rebuild , run. seems kind of bug driving me nuts , disappeared.

  3. configure host files allow local testing using emulator:

    127.0.0.1 myapplication.com api.myapplication.com

  4. to use this .net cors implementation run following nuget command on webapi project:

    install-package thinktecture.identitymodel

  5. add following class (taken post) webapi project:

    public static class corsconfig {     public static void registercors(httpconfiguration httpconfiguration)     {         webapicorsconfiguration corsconfig = new webapicorsconfiguration();         corsconfig.registerglobal(httpconfiguration);          corsconfig             .forresources("values") //controller nae             .fororigins("http://myapplication.com", "myapplication.com:81")             .allowall();     } } 
  6. call registercors() application_start()

  7. make sure web.config in webapi project has options verb extensionlessurlhandler:

      <add name="extensionlessurlhandler-integrated-4.0" path="*." verb="get,head,post,debug,put,delete,patch,options" type="system.web.handlers.transferrequesthandler" precondition="integratedmode,runtimeversionv4.0" /> 
  8. make cross-origin request hit api controller:

    $.ajax({ url: "http://api.myapplication.com/api/values", ... }) 

and viola!

hope helps others


Comments