简体   繁体   中英

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:

                        <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


                        <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 .

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. Probably 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"

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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