Angular/NET Core Web API not getting hit while running using “dotnet run” command












0















Angular newbie here. I have an ASP.NET Core 2.1 based Angular 7.0 application. My Web APIs are not hit (always get a 404) if i run my app from a command line using dotnet run, but it gets hit properly when it is run directly from VS 2017 IISExpress (by pressing F5). Since the application works fine while an F5 run, i assume my Startup.cs settings are correct, still i am copying the route part here



    app.UseMvc(
routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller}/{action}/{id?}");
});


I will be happy to provide more details, should you need them.



Update 1: I did see this post and that was not applicable in my case.



Update 2: After further investigation, i have found out that upon executing dotnet run, three ports are opened




  1. Non SSL port for ASP.NET (in my case 5000)


  2. SSL port for ASP.NET (in my case 5001)


  3. Non SSL port for Angular CLI (in my case 5002 - i specifically
    asked for 5002 in package.json)



If i make the Web API call to either 5000 or 5001 (both opened by ASP.NET), it works (while my Angular app is running in 5002!). So the questions is that how can i use the same port for both?..or is that even possible?



Update 3: Program.cs



public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

public static void Main(string args)
{
CreateWebHostBuilder(args).Build().Run();
}
}









share|improve this question

























  • Add your program.cs file - and are you using spa ?

    – Rahul
    Nov 25 '18 at 3:21











  • @RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

    – James Poulose
    Nov 25 '18 at 3:57











  • Do you enable developer exception pages? 500 error can hide behind 404

    – Anton Toshik
    Nov 25 '18 at 23:14











  • How you configure the Startup ?

    – itminus
    Nov 26 '18 at 7:42
















0















Angular newbie here. I have an ASP.NET Core 2.1 based Angular 7.0 application. My Web APIs are not hit (always get a 404) if i run my app from a command line using dotnet run, but it gets hit properly when it is run directly from VS 2017 IISExpress (by pressing F5). Since the application works fine while an F5 run, i assume my Startup.cs settings are correct, still i am copying the route part here



    app.UseMvc(
routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller}/{action}/{id?}");
});


I will be happy to provide more details, should you need them.



Update 1: I did see this post and that was not applicable in my case.



Update 2: After further investigation, i have found out that upon executing dotnet run, three ports are opened




  1. Non SSL port for ASP.NET (in my case 5000)


  2. SSL port for ASP.NET (in my case 5001)


  3. Non SSL port for Angular CLI (in my case 5002 - i specifically
    asked for 5002 in package.json)



If i make the Web API call to either 5000 or 5001 (both opened by ASP.NET), it works (while my Angular app is running in 5002!). So the questions is that how can i use the same port for both?..or is that even possible?



Update 3: Program.cs



public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

public static void Main(string args)
{
CreateWebHostBuilder(args).Build().Run();
}
}









share|improve this question

























  • Add your program.cs file - and are you using spa ?

    – Rahul
    Nov 25 '18 at 3:21











  • @RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

    – James Poulose
    Nov 25 '18 at 3:57











  • Do you enable developer exception pages? 500 error can hide behind 404

    – Anton Toshik
    Nov 25 '18 at 23:14











  • How you configure the Startup ?

    – itminus
    Nov 26 '18 at 7:42














0












0








0








Angular newbie here. I have an ASP.NET Core 2.1 based Angular 7.0 application. My Web APIs are not hit (always get a 404) if i run my app from a command line using dotnet run, but it gets hit properly when it is run directly from VS 2017 IISExpress (by pressing F5). Since the application works fine while an F5 run, i assume my Startup.cs settings are correct, still i am copying the route part here



    app.UseMvc(
routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller}/{action}/{id?}");
});


I will be happy to provide more details, should you need them.



Update 1: I did see this post and that was not applicable in my case.



Update 2: After further investigation, i have found out that upon executing dotnet run, three ports are opened




  1. Non SSL port for ASP.NET (in my case 5000)


  2. SSL port for ASP.NET (in my case 5001)


  3. Non SSL port for Angular CLI (in my case 5002 - i specifically
    asked for 5002 in package.json)



If i make the Web API call to either 5000 or 5001 (both opened by ASP.NET), it works (while my Angular app is running in 5002!). So the questions is that how can i use the same port for both?..or is that even possible?



Update 3: Program.cs



public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

public static void Main(string args)
{
CreateWebHostBuilder(args).Build().Run();
}
}









share|improve this question
















Angular newbie here. I have an ASP.NET Core 2.1 based Angular 7.0 application. My Web APIs are not hit (always get a 404) if i run my app from a command line using dotnet run, but it gets hit properly when it is run directly from VS 2017 IISExpress (by pressing F5). Since the application works fine while an F5 run, i assume my Startup.cs settings are correct, still i am copying the route part here



    app.UseMvc(
routes =>
{
routes.MapRoute(
name: "default",
template: "api/{controller}/{action}/{id?}");
});


I will be happy to provide more details, should you need them.



Update 1: I did see this post and that was not applicable in my case.



Update 2: After further investigation, i have found out that upon executing dotnet run, three ports are opened




  1. Non SSL port for ASP.NET (in my case 5000)


  2. SSL port for ASP.NET (in my case 5001)


  3. Non SSL port for Angular CLI (in my case 5002 - i specifically
    asked for 5002 in package.json)



If i make the Web API call to either 5000 or 5001 (both opened by ASP.NET), it works (while my Angular app is running in 5002!). So the questions is that how can i use the same port for both?..or is that even possible?



Update 3: Program.cs



public class Program
{
public static IWebHostBuilder CreateWebHostBuilder(string args)
{
return WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}

public static void Main(string args)
{
CreateWebHostBuilder(args).Build().Run();
}
}






angular asp.net-web-api asp.net-core routing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 3:56







James Poulose

















asked Nov 25 '18 at 0:46









James PouloseJames Poulose

1,5731319




1,5731319













  • Add your program.cs file - and are you using spa ?

    – Rahul
    Nov 25 '18 at 3:21











  • @RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

    – James Poulose
    Nov 25 '18 at 3:57











  • Do you enable developer exception pages? 500 error can hide behind 404

    – Anton Toshik
    Nov 25 '18 at 23:14











  • How you configure the Startup ?

    – itminus
    Nov 26 '18 at 7:42



















  • Add your program.cs file - and are you using spa ?

    – Rahul
    Nov 25 '18 at 3:21











  • @RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

    – James Poulose
    Nov 25 '18 at 3:57











  • Do you enable developer exception pages? 500 error can hide behind 404

    – Anton Toshik
    Nov 25 '18 at 23:14











  • How you configure the Startup ?

    – itminus
    Nov 26 '18 at 7:42

















Add your program.cs file - and are you using spa ?

– Rahul
Nov 25 '18 at 3:21





Add your program.cs file - and are you using spa ?

– Rahul
Nov 25 '18 at 3:21













@RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

– James Poulose
Nov 25 '18 at 3:57





@RahulSwamynathan Yes, this is a SPA. I have updated the question with Program.cs

– James Poulose
Nov 25 '18 at 3:57













Do you enable developer exception pages? 500 error can hide behind 404

– Anton Toshik
Nov 25 '18 at 23:14





Do you enable developer exception pages? 500 error can hide behind 404

– Anton Toshik
Nov 25 '18 at 23:14













How you configure the Startup ?

– itminus
Nov 26 '18 at 7:42





How you configure the Startup ?

– itminus
Nov 26 '18 at 7:42












1 Answer
1






active

oldest

votes


















0














I actually found the reason for this issue, but forgot to update here. My apologies.



Angular CLI is always run on a separate port. Let's assume your ASP.NET is listening on port 5000 and your Angular CLI is on 4200. Now if you are trying to call a Web API from Angular, you are looking at the CORS problem.



How i solved my problem




  • Enable CORS in ASP.NET Core by following these instructions.

  • From Angular, if you make a call to api/controller/action/id, it
    will prefix the route with http://localhost:4200/ for you and that port does not
    have ASP.NET. So i hijacked the calls and conditionally re-routed the
    calls to port 5000 based on the environment.production (defined in environmentsenvironment.ts)


Hope this helps!






share|improve this answer























    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%2f53463735%2fangular-net-core-web-api-not-getting-hit-while-running-using-dotnet-run-comman%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














    I actually found the reason for this issue, but forgot to update here. My apologies.



    Angular CLI is always run on a separate port. Let's assume your ASP.NET is listening on port 5000 and your Angular CLI is on 4200. Now if you are trying to call a Web API from Angular, you are looking at the CORS problem.



    How i solved my problem




    • Enable CORS in ASP.NET Core by following these instructions.

    • From Angular, if you make a call to api/controller/action/id, it
      will prefix the route with http://localhost:4200/ for you and that port does not
      have ASP.NET. So i hijacked the calls and conditionally re-routed the
      calls to port 5000 based on the environment.production (defined in environmentsenvironment.ts)


    Hope this helps!






    share|improve this answer




























      0














      I actually found the reason for this issue, but forgot to update here. My apologies.



      Angular CLI is always run on a separate port. Let's assume your ASP.NET is listening on port 5000 and your Angular CLI is on 4200. Now if you are trying to call a Web API from Angular, you are looking at the CORS problem.



      How i solved my problem




      • Enable CORS in ASP.NET Core by following these instructions.

      • From Angular, if you make a call to api/controller/action/id, it
        will prefix the route with http://localhost:4200/ for you and that port does not
        have ASP.NET. So i hijacked the calls and conditionally re-routed the
        calls to port 5000 based on the environment.production (defined in environmentsenvironment.ts)


      Hope this helps!






      share|improve this answer


























        0












        0








        0







        I actually found the reason for this issue, but forgot to update here. My apologies.



        Angular CLI is always run on a separate port. Let's assume your ASP.NET is listening on port 5000 and your Angular CLI is on 4200. Now if you are trying to call a Web API from Angular, you are looking at the CORS problem.



        How i solved my problem




        • Enable CORS in ASP.NET Core by following these instructions.

        • From Angular, if you make a call to api/controller/action/id, it
          will prefix the route with http://localhost:4200/ for you and that port does not
          have ASP.NET. So i hijacked the calls and conditionally re-routed the
          calls to port 5000 based on the environment.production (defined in environmentsenvironment.ts)


        Hope this helps!






        share|improve this answer













        I actually found the reason for this issue, but forgot to update here. My apologies.



        Angular CLI is always run on a separate port. Let's assume your ASP.NET is listening on port 5000 and your Angular CLI is on 4200. Now if you are trying to call a Web API from Angular, you are looking at the CORS problem.



        How i solved my problem




        • Enable CORS in ASP.NET Core by following these instructions.

        • From Angular, if you make a call to api/controller/action/id, it
          will prefix the route with http://localhost:4200/ for you and that port does not
          have ASP.NET. So i hijacked the calls and conditionally re-routed the
          calls to port 5000 based on the environment.production (defined in environmentsenvironment.ts)


        Hope this helps!







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 5:05









        James PouloseJames Poulose

        1,5731319




        1,5731319






























            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%2f53463735%2fangular-net-core-web-api-not-getting-hit-while-running-using-dotnet-run-comman%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

            Lallio

            Unable to find Lightning Node

            Futebolista