使用 ASP.NET Core 3.1,MVC 默认选择了指向启动页的端点路由,如果没有任何路由指向,将只返回 404。为了解决此问题,我们需要在 Startup.cs 中添加路由。在此示例中,我们将在 HomeController 中添加路由。
[Route("")]
[Route("[controller]")]
public class HomeController : Controller
{
// add your actions here
}
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
// add this route
endpoints.MapFallbackToController("Index", "Home");
});
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});