Angular client to IIS hosted Entity Framework CORS not allowing response












0















Configuration: Angular 6 sends an http request; it is localhost:4200. The request is to Entity Framework (standard .net) hosted on IIS at port 4000.
It sends a POST to /api/Registration/CreateNew passing the data object in the body.
The controller get functions were tested as non-CORS requests using the browser sending directly to localhost:4000 and they work properly.



The error: "Access to XMLHttpRequest at 'http://localhost:4000/api/Registration/CreateNew' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: registration:1 It does not have HTTP ok status."



The following is a fiddler snapshot of the headers Request from 4200 (Angular) and the response.



enter image description here



IIS Entity Framework WebConfig:



  <appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="true" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />

<add key="allowedCorsOrigins" value="http://localhost:4200" />
<add key="allowedCorsMethods" value="get, post, put, delete, options, batch" />
<add key="allowedCorsHeaders" value="*" />
</appSettings>


App_Start/WebApiConfig.cs



        public static void Register(HttpConfiguration config)
{
config.EnableCors();

// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}


Also added the class attribute to the Controller Class:



    [RoutePrefix("api/Registration")]
[EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
public class RegistrationController : ApiController
{


According to the Fiddler Headers shown above, it would appear that CORS requests should be happening. I have breakpoints on the IIS Controller lines and they never get hit so it would appear this (if I understand it correctly) "pre-flight" request is failing to allow the CORS request.



Unfortunately, this is a sizable chunk of code and hard to put up on the web.










share|improve this question





























    0















    Configuration: Angular 6 sends an http request; it is localhost:4200. The request is to Entity Framework (standard .net) hosted on IIS at port 4000.
    It sends a POST to /api/Registration/CreateNew passing the data object in the body.
    The controller get functions were tested as non-CORS requests using the browser sending directly to localhost:4000 and they work properly.



    The error: "Access to XMLHttpRequest at 'http://localhost:4000/api/Registration/CreateNew' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: registration:1 It does not have HTTP ok status."



    The following is a fiddler snapshot of the headers Request from 4200 (Angular) and the response.



    enter image description here



    IIS Entity Framework WebConfig:



      <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />

    <add key="allowedCorsOrigins" value="http://localhost:4200" />
    <add key="allowedCorsMethods" value="get, post, put, delete, options, batch" />
    <add key="allowedCorsHeaders" value="*" />
    </appSettings>


    App_Start/WebApiConfig.cs



            public static void Register(HttpConfiguration config)
    {
    config.EnableCors();

    // Web API configuration and services
    // Web API routes
    config.MapHttpAttributeRoutes();
    config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
    );
    }


    Also added the class attribute to the Controller Class:



        [RoutePrefix("api/Registration")]
    [EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
    public class RegistrationController : ApiController
    {


    According to the Fiddler Headers shown above, it would appear that CORS requests should be happening. I have breakpoints on the IIS Controller lines and they never get hit so it would appear this (if I understand it correctly) "pre-flight" request is failing to allow the CORS request.



    Unfortunately, this is a sizable chunk of code and hard to put up on the web.










    share|improve this question



























      0












      0








      0








      Configuration: Angular 6 sends an http request; it is localhost:4200. The request is to Entity Framework (standard .net) hosted on IIS at port 4000.
      It sends a POST to /api/Registration/CreateNew passing the data object in the body.
      The controller get functions were tested as non-CORS requests using the browser sending directly to localhost:4000 and they work properly.



      The error: "Access to XMLHttpRequest at 'http://localhost:4000/api/Registration/CreateNew' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: registration:1 It does not have HTTP ok status."



      The following is a fiddler snapshot of the headers Request from 4200 (Angular) and the response.



      enter image description here



      IIS Entity Framework WebConfig:



        <appSettings>
      <add key="webpages:Version" value="3.0.0.0" />
      <add key="webpages:Enabled" value="false" />
      <add key="ClientValidationEnabled" value="true" />
      <add key="UnobtrusiveJavaScriptEnabled" value="true" />

      <add key="allowedCorsOrigins" value="http://localhost:4200" />
      <add key="allowedCorsMethods" value="get, post, put, delete, options, batch" />
      <add key="allowedCorsHeaders" value="*" />
      </appSettings>


      App_Start/WebApiConfig.cs



              public static void Register(HttpConfiguration config)
      {
      config.EnableCors();

      // Web API configuration and services
      // Web API routes
      config.MapHttpAttributeRoutes();
      config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
      );
      }


      Also added the class attribute to the Controller Class:



          [RoutePrefix("api/Registration")]
      [EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
      public class RegistrationController : ApiController
      {


      According to the Fiddler Headers shown above, it would appear that CORS requests should be happening. I have breakpoints on the IIS Controller lines and they never get hit so it would appear this (if I understand it correctly) "pre-flight" request is failing to allow the CORS request.



      Unfortunately, this is a sizable chunk of code and hard to put up on the web.










      share|improve this question
















      Configuration: Angular 6 sends an http request; it is localhost:4200. The request is to Entity Framework (standard .net) hosted on IIS at port 4000.
      It sends a POST to /api/Registration/CreateNew passing the data object in the body.
      The controller get functions were tested as non-CORS requests using the browser sending directly to localhost:4000 and they work properly.



      The error: "Access to XMLHttpRequest at 'http://localhost:4000/api/Registration/CreateNew' from origin 'http://localhost:4200' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: registration:1 It does not have HTTP ok status."



      The following is a fiddler snapshot of the headers Request from 4200 (Angular) and the response.



      enter image description here



      IIS Entity Framework WebConfig:



        <appSettings>
      <add key="webpages:Version" value="3.0.0.0" />
      <add key="webpages:Enabled" value="false" />
      <add key="ClientValidationEnabled" value="true" />
      <add key="UnobtrusiveJavaScriptEnabled" value="true" />

      <add key="allowedCorsOrigins" value="http://localhost:4200" />
      <add key="allowedCorsMethods" value="get, post, put, delete, options, batch" />
      <add key="allowedCorsHeaders" value="*" />
      </appSettings>


      App_Start/WebApiConfig.cs



              public static void Register(HttpConfiguration config)
      {
      config.EnableCors();

      // Web API configuration and services
      // Web API routes
      config.MapHttpAttributeRoutes();
      config.Routes.MapHttpRoute(
      name: "DefaultApi",
      routeTemplate: "api/{controller}/{id}",
      defaults: new { id = RouteParameter.Optional }
      );
      }


      Also added the class attribute to the Controller Class:



          [RoutePrefix("api/Registration")]
      [EnableCors(origins: "http://localhost:4200", headers: "*", methods: "*")]
      public class RegistrationController : ApiController
      {


      According to the Fiddler Headers shown above, it would appear that CORS requests should be happening. I have breakpoints on the IIS Controller lines and they never get hit so it would appear this (if I understand it correctly) "pre-flight" request is failing to allow the CORS request.



      Unfortunately, this is a sizable chunk of code and hard to put up on the web.







      angular entity-framework iis cors






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 2:29









      Dale Burrell

      3,25932553




      3,25932553










      asked Jan 1 at 19:37









      Yogi BearYogi Bear

      207414




      207414
























          1 Answer
          1






          active

          oldest

          votes


















          0














          According to your response error message, it return 404 error, not 401 error.
          404 error means he couldn't find the page.



          As far as I know, the CORS related error is 401 as like below:



          enter image description here



          I suggest you could check your application to make sure you have used to the right url to access web api.






          share|improve this answer


























          • Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

            – Yogi Bear
            Jan 6 at 13:46











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998372%2fangular-client-to-iis-hosted-entity-framework-cors-not-allowing-response%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          According to your response error message, it return 404 error, not 401 error.
          404 error means he couldn't find the page.



          As far as I know, the CORS related error is 401 as like below:



          enter image description here



          I suggest you could check your application to make sure you have used to the right url to access web api.






          share|improve this answer


























          • Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

            – Yogi Bear
            Jan 6 at 13:46
















          0














          According to your response error message, it return 404 error, not 401 error.
          404 error means he couldn't find the page.



          As far as I know, the CORS related error is 401 as like below:



          enter image description here



          I suggest you could check your application to make sure you have used to the right url to access web api.






          share|improve this answer


























          • Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

            – Yogi Bear
            Jan 6 at 13:46














          0












          0








          0







          According to your response error message, it return 404 error, not 401 error.
          404 error means he couldn't find the page.



          As far as I know, the CORS related error is 401 as like below:



          enter image description here



          I suggest you could check your application to make sure you have used to the right url to access web api.






          share|improve this answer















          According to your response error message, it return 404 error, not 401 error.
          404 error means he couldn't find the page.



          As far as I know, the CORS related error is 401 as like below:



          enter image description here



          I suggest you could check your application to make sure you have used to the right url to access web api.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 7 at 8:42

























          answered Jan 2 at 7:54









          Brando ZhangBrando Zhang

          6,4992725




          6,4992725













          • Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

            – Yogi Bear
            Jan 6 at 13:46



















          • Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

            – Yogi Bear
            Jan 6 at 13:46

















          Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

          – Yogi Bear
          Jan 6 at 13:46





          Sorry for the delay in accepting this. It took me a while to figure out why the error actually occurred. Thanks again for your help.

          – Yogi Bear
          Jan 6 at 13:46




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998372%2fangular-client-to-iis-hosted-entity-framework-cors-not-allowing-response%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Monofisismo

          Angular Downloading a file using contenturl with Basic Authentication

          Olmecas