C# 为什么LinkGenerator.GetUriByPage();价值观;参数返回字符串。传入字符串时的长度?
我不熟悉ASP.NET核心,只是学习一些新概念 当我使用C# 为什么LinkGenerator.GetUriByPage();价值观;参数返回字符串。传入字符串时的长度?,c#,asp.net-core,C#,Asp.net Core,我不熟悉ASP.NET核心,只是学习一些新概念 当我使用 public static string GetUriByPage (this Microsoft.AspNetCore.Routing.LinkGenerator generator, string page, string handler, object values, string sc
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);
值
字段将输入作为类型对象
。如果我尝试插入一个字符串作为对象,它将采用Length
string属性的值,而不是string变量的值。我假设values
字段获取对象的所有公共属性,这就是它的工作原理,但是我如何才能只传入字符串值而不传入公共属性呢
我想我只是想了解为什么会这样,政府对幕后的行为没有太多的信息
测试代码
// 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
类构造函数。该类需要一个键/值集或一个对象,该对象的属性及其值用于创建字典。这就是为什么您会看到String.Length
您可以传递具有要解析URL的值的匿名对象。(将属性名称更改为您的方便)
您可以在
232
行中的文档中看到值
参数传递到RouteValueDictionary
的过程,这里有文档记录是的,这就是文档,但这并不能解释为什么传入字符串而不是字符串的值时,“values”参数返回String.Length。我怀疑这不是Asp.Net的核心问题,而是我不了解C#…我想我应该RTFM。。。谢谢不要对自己太苛刻…任何人在学习时都会忽略一些事情。快乐编码!
// 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);