简体   繁体   English

href 链接中重复的导航操作?

[英]Navigation actions duplicated in href links?

I have a .Net 3.1 core web app, with the following markup that I'm using as the navigation:我有一个 .Net 3.1 核心 Web 应用程序,使用以下标记作为导航:

                        <ul class="navbar-nav flex-grow-1">
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="About" asp-action="About">About</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Students" asp-action="Students">Students</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Courses" asp-action="Courses">Courses</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Instructors" asp-action="Instructors">Instructors</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link text-white" asp-area="" asp-controller="Departments" asp-action="Departments">Departments</a>
                            </li>
    
                        </ul>

However, when I run the app using the start button, the url hrefs contain the actions listed twice, eg但是,当我使用开始按钮运行应用程序时,url hrefs 包含列出两次的操作,例如


                        <li class="nav-item">
                            <a class="nav-link text-white" href="/About/About">About</a>
                        </li>
                        

instead of代替


                        <li class="nav-item">
                            <a class="nav-link text-white" href="/About">About</a>
                        </li>
                        

Are there action parameters in my controller files that I need to change?我的控制器文件中是否有需要更改的操作参数? Where is the file that contains the routing?包含路由的文件在哪里?

Thanks,谢谢,

Robert罗伯特

As @gidanmx2 pointed out, by default, routes are like controller/action .正如@gidanmx2 指出的,默认情况下,路由类似于controller/action

In your view, you've set both controller and action the same.在您看来,您已将控制器和操作设置为相同。

<a class="nav-link text-white" asp-area="" asp-controller="About" asp-action="About">About</a>

That is why you have that About/About link.这就是为什么你有关于/关于链接。

I guess you haven't created one controller for each of your action so set the correct controller to asp-controller and it should be fine.我猜你还没有为你的每个动作创建一个控制器,所以将正确的控制器设置为asp-controller应该没问题。 Probably Home ?可能是Home

By default the routes are defined in Startup.cs, as you can see there is a default action called "Index" if you rename your action "About" to "Index" then your link will be "/About"默认情况下,路由在 Startup.cs 中定义,如您所见,如果您将操作“关于”重命名为“索引”,则您可以看到有一个名为“索引”的默认操作,那么您的链接将是“/关于”

app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });

You can also decorate the Action with the Route attribute to define a custom route.您还可以使用Route属性装饰 Action 以定义自定义路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM