簡體   English   中英

將我的應用程序遷移到 ASP.NET Core 3.0 后,之前有效的索引 URL 返回 404

[英]After migrating my app to ASP.NET Core 3.0, the previously valid Index URL returns 404

一個應用程序從 ASP.NET Core 2.0 開始(我認為),然后遷移到 2.1,然后遷移到 2.2,現在我正在嘗試將其遷移到 3.0 失敗...

我閱讀並嘗試在官方遷移文檔中應用說明,據此我應該(除其他外)將services.AddMvc()替換為services.AddRazorPages()並將app.UseMvc()替換為app.UseEndpoints(endpoints => {endpoints.MapRazorPages();})如果我使用的是 Razor 頁面。 因為據我所知,我一直在使用 Razor Pages 而從來沒有成熟的 MVC,這就是我所做的。

現在以前工作的 URL 返回 HTTP 404 而不是任何內容......

例如, //Index路由會執行此操作,即使在項目目錄中有一個Pages/Index.cshtml文件以及一個Pages/Index.cshtml.cs文件。 (雖然奇怪:也許只有索引 url 失敗了 - 我只是嘗試將瀏覽器指向/Error並且它有效!)

Pages/Index.cshtml.cs內容(與工作版本保持不變):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Mon.Battle;

namespace mon.Pages
{
    public class IndexModel : PageModel
    {
        public IndexModel(IBattleManager battleManager)
        {
            // I hope I don't have to lock this dict here, I'm only reading
            configurationSerialized = battleManager.configurationSerialized;
        }

        public ConfigurationSerializedFormat configurationSerialized;

        public void OnGet()
        {

        }
    }
}

Pages/Index.cshtml也包含了一些內容,但是太長太亂了,在這里整體貼出來……但是肯定應該返回一些東西,而且在遷移到 3.0 之前它已經返回了一些東西。

然而, Index.cshtml頂部的頁面指令足夠短:

@* TODO: The site becomes ugly :( Should I start using Bootstrap, instead of trying to handcraft CSS? *@
@* Hey... I actually start to like how the site looks :) *@

@page

@using System.Text.Encodings.Web
@using Microsoft.Extensions.Configuration
@inject JavaScriptEncoder jsencoder
@inject IConfiguration conf
@using static System.Text.Json.JsonSerializer
@model IndexModel
@{
    Layout = null;
}

不幸的是,這些必須與遷移前的版本相比有所改變:即自從 3.0 刪除了 Newtonsoft.JSON,我不得不用System.Text.Json替換它。

我當前的Startup.cs (我認為我准確地應用了上述文檔中的說明):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using mon.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Mon.Chat;
using Mon.MatchMaker;
using Mon.Battle;
using Mon.Player;

namespace mon
{
    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.Configure<IdentityOptions>(options =>
            {
                options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<ApplicationUser>().AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddRazorPages();

            services.AddSignalR();

            services.AddSingleton<IBattleManager, BattleManager>();
        }

        // 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("/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseRouting();

            app.UseAuthentication();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapHub<ChatHub>("/chathub");
                endpoints.MapHub<MatchMakerHub>("/mmrhub");
                endpoints.MapHub<BattleHub>("/battlehub");
                endpoints.MapRazorPages();
            });
        }
    }
}

以前Startup.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using mon.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Mon.Chat;
using Mon.MatchMaker;
using Mon.Battle;
using Newtonsoft.Json.Serialization;
using Mon.Player;

namespace mon
{
    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.Configure<IdentityOptions>(options =>
            {
                options.User.AllowedUserNameCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            });

            services.AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(
                    Configuration.GetConnectionString("DefaultConnection")));
            services.AddDefaultIdentity<ApplicationUser>().AddRoles<IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>();

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services
                .AddSignalR()
                .AddJsonProtocol(options =>
                {
                    options.PayloadSerializerSettings.ContractResolver = new DefaultContractResolver();
                });

            services.AddSingleton<IBattleManager, BattleManager>();
        }

        // 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("/Error");
                app.UseHsts();
            }

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

            app.UseAuthentication();

            app.UseSignalR(routes =>
            {
                routes.MapHub<ChatHub>("/chathub");
                routes.MapHub<MatchMakerHub>("/mmrhub");
                routes.MapHub<BattleHub>("/battlehub");
            });

            app.UseMvc();
        }
    }
}

我究竟做錯了什么? 我錯過了什么? 為什么/Index返回 HTTP 404?

如果您要求提供更多信息,我會提供。

一旦我遇到了 my.csproj 文件的問題。 確保您的文件未列出如下:

<ItemGroup>
   <Content Remove="Views\Extractor\Insert.cshtml" />
   <Content Remove="Views\_ViewImports.cshtml" />
</ItemGroup>

當我們復制粘貼文件/更改構建操作等時,可能會發生這種情況。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM