Search code examples
c#asp.net-core-mvchttp-status-code-404asp.net-core-5.0

Simple .Net 5.0 web app getting 404s (using mvc)


.Net novice here. I am following along with a web development bootcamp and I have had a persistent issue for the last few days. I've ran it by several TA's, other students, and a few instructors but we have all been stumped thus far.

I am getting 404s when opening the localhost:5000 page in my browser or using postman. The project was generated using the dotnet new web --no-https -o ProjName command and then edited.

Here is what my Startup looks like:

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.AddMvc(options => options.EnableEndpointRouting = false);
        }

        // 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.UseMvc();
        }
    }

Here is what my controller looks like

using Microsoft.AspNetCore.Mvc;

namespace Portfolio.Controllers
{
    class HomeController : Controller
    {
        [HttpGet("")]
        public string Index() => "This is my index";
    }
}

Note, I receive a response when using the default auto generated code but not using this Controller with attribute routing pattern. If you know anything about how to resolve this it would be a great help as I am falling behind.

Things I have tried:

  • Uninstalling everything related to .net and installing a single version (5.0.100 currently, but I tried a few others as well)
  • Uninstalling and reinstalling IIS Express
  • Trying to run the project from another computer. No luck.
  • Changing the port. No luck.
  • Using a different browser. No luck.
  • Sobbing on my keyboard. No luck.
  • Remaking the same project a dozen times.

I am happy to provide any additional information and I am generally tech savvy enough to follow up on what is suggested.

Thank you


Solution

  • Specifying an empty string in the HttpGet attribute is like specifying none.

    You can define a default route pattern in the useMvcmethod:

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

    Or set a slash in the HttpGet attribute to set the root path for the method:

    HttpGet("/")