Skip to content

Route data(转载)

曾璐 edited this page Mar 14, 2017 · 1 revision

Route data基础 {controller}/{action}/{id}

Route data is data that the model binder found in a URL segment specified in the routing table. For example, the default route specifies controller, action, and id segments:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

示例

In the following URL, the default route maps Instructor as the controller, Index as the action and 1 as the id; these are route data values.
http://localhost:1230/Instructor/Index/1?courseID=2021
"?courseID=2021" is a query string value. The model binder will also work if you pass the id as a query string value:
http://localhost:1230/Instructor/Index?id=1&CourseID=2021

使用ActionLink会自动创建路由

The URLs are created by ActionLink statements in the Razor view.In the following code, the id parameter matches the default route, so id is added to the route data.

@Html.ActionLink("Select", "Index", new { id = item.PersonID  })

In the following code, courseID doesn't match a parameter in the default route, so it's added as a query string.

@Html.ActionLink("Select", "Index", new { courseID = item.CourseID })

转载自:https://docs.microsoft.com/zh-cn/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-basic-crud-functionality-with-the-entity-framework-in-asp-net-mvc-application

Clone this wiki locally