【问题标题】:ASP.NET Core MVC : this localhost page can’t be foundASP.NET Core MVC:找不到这个 localhost 页面
【发布时间】:2021-04-07 15:16:24
【问题描述】:

创建了一个带有操作方法的用户控制器。右键单击“Upsert”操作方法并添加一个视图页面。

问题是当我使用此 URL 访问浏览器时

https://localhost:44318/Admin/User/Upsert

userId 获取但显示错误

找不到这个本地主机页面

同时我的其他操作方法可以正常工作,如 UserLock、用户删除等。

public class UserController : Controller
{
    private readonly ApplicationDbContext _db;
    private readonly RoleManager<IdentityRole> _roleManager;
    private readonly UserManager<IdentityUser> _userManager;

    public UserController(ApplicationDbContext db)
    {
        _db = db;
    }

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

    public IActionResult Upsert(string userId)
    {
        ........
        return View(objFromDb);
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Upsert(ApplicationUser user)
    {
        .......
        return View(user);
    }
}

【问题讨论】:

  • 这个控制器在管理区吗,可以显示startup.cs吗?
  • Asp.net 核心 MVC 5.0 @mason
  • 嗨@SoumikMahajan,有关于这个案例的最新消息吗?

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

StartUp.cs

using BornoMala.DataAccess.Data;
using BornoMala.DataAccess.Repository;
using BornoMala.DataAccess.Repository.IRepository;
using BornoMala.Utility;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace BornoMala
{
    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.AddDatabaseDeveloperPageExceptionFilter();

            services.AddIdentity<IdentityUser, IdentityRole>().AddDefaultTokenProviders()
                .AddEntityFrameworkStores<ApplicationDbContext>();
            services.AddSingleton<IEmailSender, EmailSender>();
            services.AddControllersWithViews();
            services.AddRazorPages();

            services.AddScoped<IUnitOfWork, UnitOfWork>();
        }

        // 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.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.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{area=Customer}/{controller=Home}/{action=Index}/{id?}");
                endpoints.MapRazorPages();
            });
        }
    }
}

【讨论】:

    【解决方案2】:

    找不到这个本地主机页面

    请检查您是否以正确的文件夹结构组织控制器和视图,如下所示。

    并检查您是否将控制器与区域相关联

    [Area("Admin")]
    public class UserController : Controller
    {
        //...
    

    有关在 ASP.NET Core 中使用区域的更多信息,您可以查看此文档:

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/areas?view=aspnetcore-5.0#areas-for-controllers-with-views

    【讨论】:

    • 嗨@SoumikMahajan,您找到问题的根本原因了吗?
    • its return wrong datatype 那么,你能分享一下这个问题的实际解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 2012-04-08
    相关资源
    最近更新 更多