更改默认操作不适用于ASP.NET Core 2 [英] Changing default action doesn't work for ASP.NET Core 2

查看:104
本文介绍了更改默认操作不适用于ASP.NET Core 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最新的ASP.NET MVC CORE 2默认模板上,我尝试通过如下修改控制器和操作来更改默认操作.我期望将登录页面视为默认页面,但我遇到404 http错误.我做错了什么?

On latest ASP.NET MVC CORE 2 default template, i am trying to change default action by modifying controller and action as below. I am expecting to see login page as default but i am having 404 http error. What I am doing wrong ?

您可以在默认的ASP.NET CORE 2 MVC项目上验证此问题.

You can verify this issue on default ASP.NET CORE 2 MVC project.

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Account}/{action=Login}/{id?}");
    });
}

推荐答案

如果查看AccountController类,您会看到它装饰有Route属性,如下所示:

If you look at the AccountController class, you'll see it's decorated with a Route attribute, like so:

[Route("[controller]/[action]")]
public class AccountController : Controller

但是,如果您查看HomeController类,则会发现它不是 装饰有这样的属性:

However, if you look at the HomeController class, you'll see that it is not decorated with such an attribute:

public class HomeController : Controller

因为AccountController使用的是属性路由,将不会使用常规路由模板. docs 解释这种互斥性:

Because the AccountController is using Attribute routing, it will not be picked up using the Conventional routing template. The docs explain this mutual exclusivity:

定义属性路由的操作无法通过常规路由来实现,反之亦然.

Actions that define attribute routes cannot be reached through the conventional routes and vice-versa.

这篇关于更改默认操作不适用于ASP.NET Core 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆