我正在尝试遵循 Adam Freeman 的书“ASP .NET MVC”。在这本书中有一章作者建议将路由放入特殊配置文件App_Start/RouteConfig.cs。它看起来不错,但我正在尝试在 .Net Core 的帮助下实现它。我没有找到路线的特殊位置,所以我将路线放入Startup.cs. 但它看起来非常丑陋。也许有人知道这种情况的优雅解决方案?
这是我的代码Startup.cs
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 are here ..
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
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.UseMvc(routes =>
{
// /
routes.MapRoute(null, "", new
{
controller = "Products",
action = "List",
category = "", page = 1
});
// Page2
routes.MapRoute(null,
"Page{page}",
new
{
controller = "Products",
action = "List",
category = ""
},
new { page = @"\d+" }
);
// Category
routes.MapRoute(null,
"{category}",
new
{
controller = "Products",
action = "List",
page = 1
});
PS .Net Core 版本为 2.2
哈士奇WWW
相关分类