Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an ASP.Net MVC 4 app and am using the Url.Action helper like this: @Url.Action("Information", "Admin")

This page is used for both adding a new and edit an admin profile. The URLs are as follows:

 Adding a new:       http://localhost:4935/Admin/Information
 Editing Existing:   http://localhost:4935/Admin/Information/5 <==Admin ID

When I'm in the Editing Existing section of the site and decide that I would like to add a new admin I click on the following link:

 <a href="@Url.Action("Information", "Admin")">Add an Admin</a>

The problem however that the above link is actually going to http://localhost:4935/Admin/Information/5. This only happens when I'm in that page editing an existing admin. Anywhere else on the site it links correctly to http://localhost:4935/Admin/Information

Has anyone else seen this?

UPDATE:

RouteConfig:

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );    
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
773 views
Welcome To Ask or Share your Answers For Others

1 Answer

outgoing url in mvc generated based on the current routing schema.

because your Information action method require id parameter, and your route collection has id of your current requested url(/Admin/Information/5), id parameter automatically gotten from existing route collection values.

to solve this problem you should use UrlParameter.Optional:

 <a href="@Url.Action("Information", "Admin", new { id = UrlParameter.Optional })">Add an Admin</a>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...