简体   繁体   中英

Why does LinkGenerator.GetUriByPage() “values” parameter return string.Length when a string is passed in?

I'm new to ASP.NET Core, just learning some new concepts...

When I generate a link using

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);

the values field takes an input as type object . If I try to insert a string as the object, it will take the value of the Length string property instead of the value of the string variable. I'm assuming the values field gets all the public properties of object, and that's how it works, but how would I be able to pass in just the string value and not the public properties?

I'm trying to I just want to understand why this is the case, the documentation doesn't have much info on the behaviour behind-the-scenes.

Test Code

// 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);

The values parameter is passed to RouteValueDictionary class constructor. That class expects a key/value set or an object which properties, and their values, are used to create the dictionary. That is why you see String.Length .

You can pass an anonymous object with the value you want to resolve the URL. (Change the property name to your convenience)

// 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);

You can see the passing of values parameters to RouteValueDictonary in the documentation in line 232 here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM