locked
Why my webapi no works but reports 404 no found? RRS feed

  • Question

  • User1052024640 posted

    I still follow the tutorial in this: https://www.youtube.com/watch?v=aIkpVzqLuhA

    Here is startup.cs:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;

    namespace WebApplication1
    {
    public class Startup
    {
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
    services.AddControllers();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
    if (env.IsDevelopment())
    {
    app.UseDeveloperExceptionPage();
    }
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
    endpoints.MapGet("/", async context =>
    {
    await context.Response.WriteAsync("Hello World!");
    });
    });
    }
    }
    }

    and here is the controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;

    // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

    namespace WebApplication1.Controller
    {
    [Route("api/[controller]")]
    public class ValuesController : Microsoft.AspNetCore.Mvc.ControllerBase
    {
    // GET: api/<controller>
    [HttpGet]
    public IEnumerable<string> Get()
    {
    return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
    return "value";
    }

    // POST api/<controller>
    [HttpPost]
    public void Post([FromBody]string value)
    {
    }

    // PUT api/<controller>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
    }
    }

    After I ran the URL https://localhost:44361/api/values/ , nothing works but receive a 404 Not Found.

    I am using the .net core 3.0 preview 6 and I know it differs from the .net core 2.0 much. But I want how to solve it in .net core 3.0. Thank you.

    Wednesday, June 19, 2019 12:02 PM

Answers

  • User475983607 posted

    To add, I created a Web API project from the Visual Studio 2019 Preview using the API template and the startup contains everything needed to get up and running with Web API.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    namespace Api30Demo
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 19, 2019 2:18 PM

All replies

  • User475983607 posted

    A 404 (Not Found) means the the action was not found.  Are you sure the port is correct for the Web API application?

    Perhaps follow tutorials for Core 3.0 rather than 2.0 if you are building a 3.0 application.

    https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-3.0

    Wednesday, June 19, 2019 12:46 PM
  • User1052024640 posted

    I am a beginner of .net core . I have read the tutorial you provide yet, but I feel it is so complicated, not easy for a beginner.

    So I tried to find an easy tutorial and finally found the tutorial which Microsoft provides in Youtube.

    It is unbelievable that the codes above work in .net core 2.2 but not in 3.0 .

    The 3.0 RC version will publish next month in the roadmap of Microsoft so I don't want to learn the 2.2 any longer.

    Wednesday, June 19, 2019 12:54 PM
  • User-474980206 posted
    People have expressed desire that the default configuration not include mvc and razor, so this change has been made for asp.net core 3.0. In 3.0 you must add mvc support in configuration (.AddMvc()) for controllers to be supported.
    Wednesday, June 19, 2019 2:07 PM
  • User475983607 posted

    To add, I created a Web API project from the Visual Studio 2019 Preview using the API template and the startup contains everything needed to get up and running with Web API.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.HttpsPolicy;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    using Microsoft.Extensions.Logging;
    
    namespace Api30Demo
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllers();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                    app.UseHsts();
                }
    
                app.UseHttpsRedirection();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllers();
                });
            }
        }
    }
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, June 19, 2019 2:18 PM
  • User1052024640 posted

    I just found a strange solution by disabling the EndpointRouting:

    public void ConfigureServices(IServiceCollection services)
    {
    services.AddMvc(options => options.EnableEndpointRouting = false);
    }

    However, I don't know if it is good to disable the EndpointRouting. I am a WPF developer and now feeling it greatly differs from WPF develop.

    What's more, I am interested in what you said that people have expressed that the default configuration does not include MVC and razor. Why do people want to do this?

    Wednesday, June 19, 2019 2:25 PM
  • User1052024640 posted

    Well, it works. It seems the main point is to add the 

    endpoints.MapControllers();

    I did this and it works.

    Wednesday, June 19, 2019 2:28 PM
  • User475983607 posted

    Well, it works. It seems the main point is to add the 

    endpoints.MapControllers();
    

    I did this and it works.

    The key take way is to create a new Web API project using the standard SDK 3.0 templates or reading the ASP.NET Core 3.0 Web API reference documentation. 

    Wednesday, June 19, 2019 3:00 PM
  • User1052024640 posted

    Thank you so much.

    Thursday, June 20, 2019 3:13 AM