ASP.NetCore 2.1提供HTTP404:未找到-服务器未找到与请求的URI(统一资源标识符)匹配的任何内容

时间:2019-03-02 13:55:25

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

我在MVC .netcore 2.1中建立了一个测试网站,该网站的开发工作正常,并在本地IIS上发布。当我运行发布的网站时,它显示登录页面,但是在输入用户名和密码后,它显示空白页面而不是主页。我不知道这是什么错误,将不胜感激。

通过检查F12,发现以下错误消息:

IE边缘中 “ HTTP404:未找到-服务器未找到与请求的URI(统一资源标识符)匹配的任何内容。 POST-https://localhost/

Firefox中: “未声明纯文本文档的字符编码。如果文档包含来自US-ASCII范围之外的字符,则在某些浏览器配置中,文档将呈现乱码。需要在传输中声明文件的字符编码协议或文件需要使用字节顺序标记作为编码签名。”

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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.Stores.MaxLengthForKeys = 128)
             .AddEntityFrameworkStores<ApplicationDbContext>()
             .AddDefaultUI()
             .AddDefaultTokenProviders();       

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

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

       AutoMapper.Mapper.Initialize( x=>x.AddProfile<ProfileMappings>());
    }
}

AccountController.cs 当我们提交登录表单并在所有有效时重定向时,将调用此Action。

   [HttpPost]
    public IActionResult Index(AccountLoginModel model)
    {       
        if (ModelState.IsValid)
        {                
            var user = Task.Run(async () => await userManager.FindByEmailAsync(model.Email)).Result;
            if (user != null && user.IsEnabled)
            {
                var result = repository.SignIn(model.Email, model.Password).Result;
                if (result.Succeeded)
                {
                    return RedirectToAction("Index", "Home");
                }
                ModelState.AddModelError("CustomError", "Username or/and Password is invalid.");
            }
            else
            {
                ModelState.AddModelError("CustomError", "Username or/and Password is invalid.");
            }
        }
        return View(model);
    }

谢谢

0 个答案:

没有答案