簡體   English   中英

單擊routelink URL不會將所有數據傳遞給操作

[英]Clicking on routelink url not passing all the data to action

我已經使用RouteLink創建URL。

@Html.RouteLink("Edit", "PageWithId",
new
{
    controller = "Customers",
    action = "Edit",
    id = item.CustomerID,
    page = ViewBag.CurrentPage
}) 

我正在將此路由PageWithId與路由鏈接一起使用

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
);

我有3個路由代碼。 這就是全部

routes.MapRoute(
    name: "PageWithSort",
    url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}",
    defaults: new { action = "Index", page = UrlParameter.Optional, SortColumn = UrlParameter.Optional, CurrentSort = UrlParameter.Optional }
);

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
);

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

當我運行程序時,路由鏈接會生成類似http://localhost:55831/Customers/Edit/1/ALFKI URL

當我單擊鏈接時,將調用“編輯”操作,但客戶ID卻為null ,因為URL中有ALFKI作為客戶ID。

這是我的編輯操作詳細信息

public ActionResult Edit(string id, int page)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }
    Customer customer = db.Customers.Find(id);
    if (customer == null)
    {
        return HttpNotFound();
    }
    ViewBag.CurrentPage = page;
    return View(customer);
}

請告訴我,當ALFKI作為客戶ID傳遞時,為什么id會為null?

占位符(例如{page}{controller} )的作用類似於變量。 它可以匹配任何內容並將其存儲在具有相同名稱的路由值中。

因此,您實際上對配置說的是:

嘗試

/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果這不起作用,請嘗試

/<anything (optional)>/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果這不起作用,請嘗試

/<anything (optional)>/<anything (optional)>/<anything (optional)>

如果這不起作用,則由於{id}是可選的,請嘗試

/<anything (optional)>/<anything (optional)>

如果這樣不起作用,由於{action}是可選的,請嘗試

/<anything (optional)> (if it matches, action will be "Index")

如果這不起作用,則由於{controller}是可選的,請嘗試

/ (if it matches, controller will be "Home" and action will be "Index")

看到問題了嗎? 第一條路由可以(並且將)匹配任何值且長度在0到5段之間的URL。 一旦匹配,就無法嘗試遵循它的另一條路線。

您需要以某種方式限制路由,以確保某些URL可以傳遞到下一條嘗試的路由,即在您不想匹配該URL的情況下。

同樣,可以將段設為可選 ,但不能更改position 如果傳遞的值表示CurrentSort ,則自動表示它左側的所有參數都是必需的

在我看來,將{page}{SortColumn}{CurrentSort}設為可選是不合邏輯的。 如果不需要它們,則可以使用其他路由,也可以將它們完全從路徑中移出,而使用查詢字符串(路由不受查詢字符串值的影響,它們僅在路徑上匹配)。

要使值成為必需, value = UrlParameter.Optional從默認value = UrlParameter.Optional刪除value = UrlParameter.Optional 實際上,您可以刪除所有默認值,因為控制器和操作已在URL中傳遞。

routes.MapRoute(
    name: "PageWithSort",
    url: "{controller}/{action}/{page}/{SortColumn}/{CurrentSort}"
);

routes.MapRoute(
    name: "PageWithId",
    url: "{controller}/{action}/{page}/{id}"
);

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

盡管這些路由仍然可以匹配任何內容 ,但這會將它們限制為特定的長度,並且由於所有這三個路由都具有不同數量的細分,因此它們不會沖突。 但是,由於它們仍然如此廣泛,因此您應該考慮使用另一種技術來約束它們,例如文字段或路線約束。 例如:

routes.MapRoute(
    name: "CustomerEditWithId",
    url: "Customers/Edit/{page}/{id}",
    defaults: new { controller = "Customers", action = "Edit" }
);

應用第一個路由PageWithSort而不是第二個路由。

請注意,路由框架不知道您用於生成URL的參數名稱。 它只是查看收到的字符串Customers/Edit/1/ALFKI

對於此字符串,第一個路由“ PageWithSort”與

  • 控制器=客戶
  • 動作=編輯
  • 頁= 1
  • SortColumn = ALFKI
  • CurrentSort = null(這是可以的,因為它是可選的)

因為將使用第一個匹配的路由,所以該URL將永遠不會命中“ PageWithId”路由。

也許可以使“ PageWithSort”的SortColumnCurrentSort參數成為必需?

或更改定義路由的順序。

或添加“ PageWithSort”路由特有的內容,例如

routes.MapRoute(
        name: "PageWithId",
        url: "{controller}/{action}/{page}/ID/{id}",
        //                                 ^^
        defaults: new { controller = "Customers", action = "Edit", page = UrlParameter.Optional, id = UrlParameter.Optional }
    );

/ID添加為URL的硬編碼部分將使其與其他路由區分開(假設沒有SortColumn調用“ ID”)。

暫無
暫無

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

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