簡體   English   中英

c#中參數是如何傳遞給URL的?

[英]How are parameters passed to URL in c#?

我想知道如何將參數附加到請求 URL。 假設我有一個帶有一些鏈接的表單:

在此處輸入圖片說明

當我點擊史蒂夫的“編輯”時,我得到了以下網址:

在此處輸入圖片說明

在 URL 末尾附加“3”作為 StudentID。 它是如何發生的?

我檢查我的 RouteConfig:

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Student",
                url: "student/{id}",
                defaults: new { controller = "Student", action = "Index"}
            );


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

'student/' 旁邊的 {id} 可以替換為任何內容,名稱在這里無關緊要。

我檢查我的視圖:

@model IEnumerable<TestAPP.Models.Student>


@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.StudentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Age)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.StudentName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Age)
            </td>
            <td>
                @Html.ActionLink("Edit", "Edit", new { id = item.StudentId }) |
                @Html.ActionLink("Details", "Details", new { id = item.StudentId }) |
                @Html.ActionLink("Delete", "Delete", new { id = item.StudentId })
            </td>
        </tr>
    }


</table>

即使我可以看到分配給“編輯”鏈接的“StudentId”項目(顯示在我的代碼底部),但我不知道當我單擊“編輯”時是否以某種方式將“Id”參數附加到 URL 的位置

@Html.ActionLink("Edit", "Edit", new { id = item.StudentId })

創建與每個“項目”的編輯頁面相關聯的 url。 @Html.ActionLink是 razor 頁面庫的指令,它執行將參數連接到 url 字符串的函數。 new { id = item.StudentId }根據文檔為函數提供路由值。 您的路由器需要一個可選的{id}參數來路由到特定資源,它會在 uri 的末尾查找此值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM