[英]Why does LinkGenerator.GetUriByPage() “values” parameter return string.Length when a string is passed in?
我是 ASP.NET 內核的新手,只是學習一些新概念......
當我使用生成鏈接時
public static string GetUriByPage (this Microsoft.AspNetCore.Routing.LinkGenerator generator,
string page, string handler, object values,
string scheme, Microsoft.AspNetCore.Http.HostString host,
Microsoft.AspNetCore.Http.PathString pathBase = default,
Microsoft.AspNetCore.Http.FragmentString fragment = default,
Microsoft.AspNetCore.Routing.LinkOptions options = default);
values
字段將輸入作為類型object
。 如果我嘗試插入一個字符串作為 object,它將采用Length
字符串屬性的值而不是字符串變量的值。 我假設values
字段獲取 object 的所有公共屬性,這就是它的工作原理,但是我如何能夠只傳遞字符串值而不是公共屬性?
我試圖我只是想了解為什么會這樣, 文檔沒有太多關於幕后行為的信息。
測試代碼
// Create a new entry in the hash table
var record = _context.HashRecords.Add(new Data.HashRecord {
FullName = user.FullName,
Representative = repName,
Hash = GetUniqueKey(8)
});
_context.SaveChangesAsync();
string generatedLink = _linkGenerator.GetUriByPage("/Page",
handler: null,
values: record.Entity,
scheme: _httpContextAccessor.HttpContext.Request.Scheme,
host: _httpContextAccessor.HttpContext.Request.Host);
values
參數被傳遞給RouteValueDictionary
class 構造函數。 class 需要一個鍵/值集或一個 object 哪些屬性及其值用於創建字典。 這就是您看到String.Length
的原因。
您可以傳遞一個匿名 object 和您想要解析 URL 的值。 (更改屬性名稱以方便您)
// Create a new entry in the hash table
var record = _context.HashRecords.Add(new Data.HashRecord {
FullName = user.FullName,
Representative = repName,
Hash = GetUniqueKey(8)
});
_context.SaveChangesAsync();
string generatedLink = _linkGenerator.GetUriByPage("/Page",
handler: null,
// Passing an object to be converted to dictionary
values: new { entity = record.Entity },
scheme: _httpContextAccessor.HttpContext.Request.Scheme,
host: _httpContextAccessor.HttpContext.Request.Host);
您可以在此處第232
行的文檔中看到將values
參數傳遞給RouteValueDictonary
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.