在 .NET Core 中定义多个区域的路由可以通过以下步骤实现:
using Microsoft.AspNetCore.Mvc;
namespace YourProject.Areas.Admin
{
public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName => "Admin";
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
name: "Admin_default",
template: "Admin/{controller=Home}/{action=Index}/{id?}"
);
}
}
}
public void ConfigureServices(IServiceCollection services)
{
// Other configurations...
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Admin", "/Home", "admin");
});
// Other configurations...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Other configurations...
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
routes.MapAreaRoute(
name: "Admin",
areaName: "Admin",
template: "Admin/{controller=Home}/{action=Index}/{id?}");
});
// Other configurations...
}
通过以上步骤,在 .NET Core 中就成功定义了一个名为 "Admin" 的区域,并为该区域配置了路由。在访问该区域的控制器和视图时,可以使用 "/Admin/Controller/Action" 的 URL 格式来访问。
领取专属 10元无门槛券
手把手带您无忧上云