Asp.Net核心路由无法正常工作

时间:2016-10-19 15:15:40

标签: routing asp.net-core asp.net-core-mvc

我搜索了很多问题,似乎没有人能解决我的具体问题。

我正在阅读一些教程,[Route("Text")]在教程中不起作用。

所以使用此代码:

 public class AboutController
    {
        public string Copyright() {
            return "\u00a9 Copyright are the best";
        }

        public string Year() {
            return "2016";
        }
    }

我可以毫无问题地前往localhost:xxx/about/copyrightlocalhost:xxx/about/year。 但是,如果我在声明AboutController之前立即尝试添加[路线("什么")],我现在无法导航到localhost:xxx/what/copyrightlocalhost:xxx/what/year。 但是如果我试试这个:

 [Route("what/[action]")]
    public class AboutController
    {
        [Route("rights")]
        public string Copyright() {
            return "\u00a9 Copyright are the best";
        }

        public string Year() {
            return "2016";
        }
    }

我可以转到localhost:xxx/what/year但不能转到localhost:xxx/what/rights(或localhost:xxx/what/copyright

那么,我错过了什么?

1 个答案:

答案 0 :(得分:1)

[Route("rights")]添加到Copyright()时,会将其解释为localhost:xxx/what/copyright/rights

所以使用你的上一个例子:

 [Route("what/[action]")]//url is: localhost:xxx/what/someactionname
public class AboutController
{
    [Route("rights")]//url is: localhost:xxx/what/copyright/rights --since CopyRight() is the action
    public string Copyright() {
        return "\u00a9 Copyright are the best";
    }

    public string Year() { //localhost:xxx/what/year --since Year() is the action
        return "2016";
    }
}