Click here to Skip to main content
15,920,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In ASP.NET MVC while running the application it throws an error over the browser.
This localhost page can’t be found No webpage was found for the web address: https://localhost:7011/ HTTP ERROR 404


What I have tried:

Startup.cs
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using SpiceProj.Data;
using Stripe;

namespace SpiceApplication
{
    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.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<IdentityUser>(options =>
            {
                bool requireConfirmedAccount = options.SignIn.RequireConfirmedAccount;
            })
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddControllersWithViews();
            services.AddRazorPages().AddRazorRuntimeCompilation();

        }

        // 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.UseDatabaseErrorPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // 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.UseRouting();
               StripeConfiguration.ApiKey = Configuration.GetSection("Stripe")["SecretKey"];
            //  dbInitializer.Initialize();
            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseSession();
            app.UseAuthentication();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
           
        }
    }
}


HomeController.cs

using Microsoft.AspNetCore.Mvc;
using SpiceProj.Models;
using System.Diagnostics;

namespace SpiceProj.Areas.Customer.Controllers
{
    [Area("Customer")]
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public HomeController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public IActionResult Privacy()
        {
            return View();
        }

        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}


Program.cs

using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using SpiceProj.Data;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options =>
    options.UseSqlServer(connectionString));
builder.Services.AddDatabaseDeveloperPageExceptionFilter();

builder.Services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
    .AddEntityFrameworkStores<ApplicationDbContext>();
builder.Services.AddControllersWithViews();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseMigrationsEndPoint();
}
else
{
    app.UseExceptionHandler("/Home/Error");
    // 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.UseStaticFiles();

app.UseRouting();

app.UseAuthentication();
app.UseAuthorization();

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

app.Run();
Posted
Updated 7-Sep-22 2:40am
Comments
Richard MacCutchan 5-Sep-22 7:41am    
The error message is clear, you do not have a web server listening on that address.
Vikas Goswami0297 5-Sep-22 8:22am    
Thankyou for the response. I would like to know the further steps for the web server listening on that address what to do Please help me or guide me.
Richard MacCutchan 5-Sep-22 8:52am    
You need to provide more information. We have no way of knowing what your server is running on or why you are trying to get a page from that address.

1 solution

You're mixing code from two different styles of application. You have the Startup class from .NET Core, and the top-level statements from .NET 6.

Since your top-level statements never call it, your Startup class will not be used. Therefore, the line which defines the default route pointing to the HomeController in the Customer are will never be executed.

Instead, the routes defined by your top-level statements will be used. That sets the default route to the HomeController in the root area, which doesn't exist.

Remove your Startup class, since it will just cause confusion. Move any relevant settings from that class to your top-level statements in Program.cs. Then either adjust your routes to specify the default area, or remove the area from the HomeController class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900