4

恐怕我已经束手无策了,找不到解决办法。我已经用尽了我的 Google-fu 能力,现在不得不寻求帮助。恐怕我是新手,这可能很简单,这是我用来学习的一个小项目。

切入正题。我将 ASP.NET Core 3.1 与使用端点的 MVC 控制器一起使用。我想将我的区域单独添加到 startup.cs 中的端点路由。我在我的 _ViewImports 中设置了 TagHelpers,其他视图正在工作。

问题:在我的具有分页列表的图书编辑器中,链接缺少该区域Dashboard/,如果我在与它们交互时asp-area="Dashboard"放入<a></a>表单链接,我会生成此 URL:

https://localhost:5001/LibraryTomes/TomeLanguageEditor?tomelanguid=dd0186f7-2ff4-46d1-b69c-3eaae0f91331&page=2&area=Dashboard

所以而不是:

https://localhost:5001/Dashboard/LibraryTomes/TomeLanguageEditor?tomelanguid=dd0186f7-2ff4-46d1-b69c-3eaae0f91331&page=2

我正在附加该区域&area=Dashboard。但是,我发现了一些文章,它们似乎都只解决了我不想要的 {area:exists} 问题。

以下是我的 Startup.cs,app.UseEndpoints(endpoints =>我相信这是正确的,因为我想摆脱area:exists.

namespace TestProject
{
    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.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940

        public class UntrustedCertClientFactory : DefaultHttpClientFactory
        {
            public override HttpMessageHandler CreateMessageHandler()
            {
                return new HttpClientHandler
                {
                    ServerCertificateCustomValidationCallback = (a, b, c, d) => true
                };
            }
        }

        public void ConfigureServices(IServiceCollection services)
        {

            services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

            FlurlHttp.ConfigureClient("https://127.0.0.1", cli => cli.Settings.HttpClientFactory = new UntrustedCertClientFactory()); //Ignore test certs

            var settingsfile = File.ReadAllText(@"settings.json");
            var deserialjson = JsonConvert.DeserializeObject<SettingsJson>(settingsfile);

            if (deserialjson.DatabaseProvider == DBProviderWrittenSetJson.MSSQL)
            {
                var connection = Configuration[@"MSSQLDBConnection"];
                services.AddDbContext<TestProjectContext>
                    (options => options.UseSqlServer(connection,
                    x => x.EnableRetryOnFailure())
                    );
                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddRoles<IdentityRole>()
                    .AddEntityFrameworkStores<TestProjectContext>();
            }
            if (deserialjson.DatabaseProvider == DBProviderWrittenSetJson.PostgreSQL)
            {
                var connection = Configuration[@"PostgreSQLConnection"];
                services.AddDbContext<TestProjectContext>
                    (options => options.UseNpgsql(connection,
                    x => x.EnableRetryOnFailure())
                    );
                services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
                    .AddRoles<IdentityRole>()
                    .AddEntityFrameworkStores<TestProjectContext>();
            }           

            services.AddControllersWithViews()
                .AddNewtonsoftJson();
            services.AddRazorPages();
            services.AddServerSideBlazor();

        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, TestProjectContext context)
        {
            app.UseForwardedHeaders(new ForwardedHeadersOptions
            {
                ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
            });

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();
            app.UseRouting();
            app.UseAuthentication();
            app.UseAuthorization();

            var settingsfile = File.ReadAllText(@"settings.json");
            var deserialjson = JsonConvert.DeserializeObject<SettingsJson>(settingsfile);

            app.UseEndpoints(endpoints =>
                    {
                        endpoints.MapControllers();
                        endpoints.MapRazorPages();
                        endpoints.MapBlazorHub();

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

                        endpoints.MapAreaControllerRoute(
                            name: "Dashboard",
                            areaName: "Dashboard",
                            pattern: "{area=Dashboard}/{controller=Home}/{action=Index}/{id?}",
                            defaults: new { area = "Dashboard", controller = "Home", action = "Index" });

                    });

        }
    }
}

我一直在尝试使用路由做很多事情,并尝试使用 HttpGet。

namespace TestProject.Areas.Dashboard.Controllers
{
    [Area("Dashboard")]
    [Route("Dashboard/LibraryTomes")]
    public class LibraryTomesController : Controller
    {

        //[HttpGet("TomeLanguageEditor")]
        [Route("TomeLanguageEditor", Name = "TomeLanguageEditor")]
        public async Task<IActionResult> TomeLanguageEditor(string? tomelanguid, string sortOrder, string currentFilter, string searchString, LibraryViewModel? libraryViewModel, int? page) 

我将创建一个简单的演示来展示我所看到的,还有更多!

4

0 回答 0