如何在 ASP.NET Core MVC for Identity 中添加自定义路由?
How to add custom routes in ASP.NET Core MVC for Identity?
我习惯于使用 ASP.NET MVC 框架,但现在我已经转向 ASP.NET 核心 MVC,路由似乎是另外一回事。
这是项目结构(身份是脚手架)
这就是我添加自定义路由的方式,就像在经典 ASP.NET MVC 中一样:
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
在此示例中,我使用 https://localhost/About
而不是 https://localhost/Home/About
我正在尝试使用 ASP.NET 核心 MVC 实现相同的目的,但我找不到任何与此相关的文章,并且“url”属性不再存在。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Charts}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Register",
pattern: "{controller=Identity/Account}/{action=Register}");
endpoints.MapRazorPages();
});
我想添加到 https://localhost/Identity/Account/Register
到 https://localhost/Register
的自定义路由
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using ERP_MKM.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ERP_MKM.Models;
using Microsoft.AspNetCore.Identity.UI.Services;
using ERP_MKM.Services;
namespace ERP_MKM
{
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.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityCore<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI();
services.AddTransient<IEmailSender, EmailSender>(i =>
new EmailSender(
Configuration["EmailSender:Host"],
Configuration.GetValue<int>("EmailSender:Port"),
Configuration.GetValue<bool>("EmailSender:EnableSSL"),
Configuration["EmailSender:UserName"],
Configuration["EmialSender:Password"]
)
);
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();
}
// 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("/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.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Charts}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Register",
pattern: "Register",
defaults: new { area = "Identity", controller = "Account", action = "Register" });
endpoints.MapRazorPages();
});
}
}
}
控制器映射
endpoints.MapControllerRoute(
name: "any-route-name",
pattern: "register",
defaults: new { area = "Identity", controller = "Account", action = "Register" }
);
对于 Razor 页面,有 AddPageRoute
和 AddAreaPageRoute
方法。此代码应在 ConfigureServices
方法
内
services.AddRazorPages(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Register", "/Register");
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/Login");
});
并且不要忘记使用代码行来启用端点中的页面
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages(); // this one
});
您可以使用 Visual Studio 搭建页面以在您的项目中包含和修改它。
Select 所需页面,select 用户上下文,然后单击“确定”。此屏幕截图对于找出身份区域中页面的所有路径也很有用。
我习惯于使用 ASP.NET MVC 框架,但现在我已经转向 ASP.NET 核心 MVC,路由似乎是另外一回事。
这是项目结构(身份是脚手架)
这就是我添加自定义路由的方式,就像在经典 ASP.NET MVC 中一样:
routes.MapRoute(
name: "About",
url: "About",
defaults: new { controller = "Home", action = "About" }
);
在此示例中,我使用 https://localhost/About
而不是 https://localhost/Home/About
我正在尝试使用 ASP.NET 核心 MVC 实现相同的目的,但我找不到任何与此相关的文章,并且“url”属性不再存在。
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Charts}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Register",
pattern: "{controller=Identity/Account}/{action=Register}");
endpoints.MapRazorPages();
});
我想添加到 https://localhost/Identity/Account/Register
到 https://localhost/Register
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.EntityFrameworkCore;
using ERP_MKM.Data;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using ERP_MKM.Models;
using Microsoft.AspNetCore.Identity.UI.Services;
using ERP_MKM.Services;
namespace ERP_MKM
{
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.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddIdentityCore<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI();
services.AddTransient<IEmailSender, EmailSender>(i =>
new EmailSender(
Configuration["EmailSender:Host"],
Configuration.GetValue<int>("EmailSender:Port"),
Configuration.GetValue<bool>("EmailSender:EnableSSL"),
Configuration["EmailSender:UserName"],
Configuration["EmialSender:Password"]
)
);
services.AddControllers().AddNewtonsoftJson(options =>
options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
);
services.AddControllersWithViews();
services.AddRazorPages().AddRazorRuntimeCompilation();
}
// 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("/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.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Charts}/{action=Index}/{id?}");
endpoints.MapControllerRoute(
name: "Register",
pattern: "Register",
defaults: new { area = "Identity", controller = "Account", action = "Register" });
endpoints.MapRazorPages();
});
}
}
}
控制器映射
endpoints.MapControllerRoute(
name: "any-route-name",
pattern: "register",
defaults: new { area = "Identity", controller = "Account", action = "Register" }
);
对于 Razor 页面,有 AddPageRoute
和 AddAreaPageRoute
方法。此代码应在 ConfigureServices
方法
services.AddRazorPages(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Register", "/Register");
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "/Login");
});
并且不要忘记使用代码行来启用端点中的页面
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapRazorPages(); // this one
});
您可以使用 Visual Studio 搭建页面以在您的项目中包含和修改它。
Select 所需页面,select 用户上下文,然后单击“确定”。此屏幕截图对于找出身份区域中页面的所有路径也很有用。