【发布时间】:2020-07-17 06:17:54
【问题描述】:
我在 asp.net core 3.1 Razor 页面 Web 应用程序中创建了几个页面用于教育目的,因为我是 asp.net 核心的新手,我面临着页面asp-page="/Admin/News" 的一个路由的奇怪问题,所有其他路由都有效很好,但是当我为管理页面 asp-page="/Admin/News" 添加另一个链接时,它不起作用它会显示导航项目,但总是指向我所在的同一页面,例如,如果我在 Employee 上并且如果我点击管理部分it will go to Employee`,就像我在这个页面上一样。
不知道为什么会这样,我将asp-page="/Admin/News" 更改为asp-page="/Admin/News/"、asp-page="/Admin/News/Index",即使这些路线也不起作用
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/BookList/Index">Book</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Employee">Employee</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/News">News</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-page="/Admin/News">Admin Section</a>
</li>
</ul>
如果我直接输入页面网址https://localhost:44364/Admin/News/,我就可以访问页面
Startup.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BookListRazor.Data;
using BookListRazor.Model;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace BookListRazor
{
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.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddDbContext<ApplicationDbContext>(option => option.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
var sqlConnectionConfiguration = new SqlConnectionConfiguration(Configuration.GetConnectionString("DefaultConnection"));
services.AddSingleton(sqlConnectionConfiguration);
//services.AddTransient<IEmployeeService, EmployeeDapperService>();
services.AddScoped<IEmployeeService, EmployeeDapperService>();
services.AddScoped<INewsService, NewsService>();
}
// 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();
}
else
{
app.UseExceptionHandler("/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.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}
}
【问题讨论】:
-
/Admin/News/Index 应该可以工作,请显示您的 startup.cs。
-
@NanYu,我已经更新了有关 startup.cs 详细信息的问题。它在互联网上寻找任何参考,但找不到任何解决方案,很少有选项建议添加
asp-route-roleId="1" asp-route-userId="2",即使这样也能正常工作。现在正在使用它作为简单的锚,它可以工作<a class="nav-link text-dark" href="/Admin/News/" >Admin Section</a>直到我发现它有什么问题
标签: asp.net asp.net-core razor-pages asp.net-core-3.1