本文整理汇总了C#中Address.ContainsKey方法的典型用法代码示例。如果您正苦于以下问题:C# Address.ContainsKey方法的具体用法?C# Address.ContainsKey怎么用?C# Address.ContainsKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Address
的用法示例。
在下文中一共展示了Address.ContainsKey方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetRouteDatas
/// <summary>
/// Get the possible route datas that could have selected a given container or content item via a route
/// </summary>
/// <param name="route">The route</param>
/// <param name="o">The container or content item</param>
/// <returns>The route datas that could have accessed the object via the route</returns>
public List<RouteData> GetRouteDatas(Route route, Address address)
{
// Start list of urls which could map to this item. Note, catchall items (starting *) are treated as normal ones
List<RouteValueDictionary> rvds = new List<RouteValueDictionary>();
var keyOutputs = route.KeyOutputs();
Type type = address.Type;
ICollator collator = Collator.Instance.Registered(type);
var rvd = new RouteValueDictionary(address);
// Action & controller
if (!(keyOutputs.ContainsKey("controller") && keyOutputs.ContainsKey("action")))
throw new Exception("Route " + route.Url + " fails to define controller and action");
if (keyOutputs["controller"].StartsWith("?"))
throw new Exception("Route " + route.Url + " is a data route which lacks but must have a specified controller");
if (!ContentTypeHierarchy.ControllerActions.ContainsKey(keyOutputs["controller"]))
return new List<RouteData>(); // Controller doesn't exist therefore no rvds
rvd.Add("controller", keyOutputs["controller"]);
rvds.Add(rvd);
// copy list so we can change 'actions'
var actions = ContentTypeHierarchy.ControllerActions[keyOutputs["controller"]].ToList();
if (keyOutputs["action"].StartsWith("?"))
{
if (keyOutputs["action"].Length > 1 && actions.Contains(keyOutputs["action"].Substring(1).ToLower()))
actions.Add("??");
rvds = PermuteAdd(rvds, "action", actions);
}
else if (!actions.Contains(keyOutputs["action"]))
return new List<RouteData>(); // Fixed action doesn't exist therefore no urls
else
rvd.Add("action", keyOutputs["action"]);
// Route vars addressing the content item
foreach (var kvp in keyOutputs)
{
if (kvp.Key == "controller" || kvp.Key == "action") // already dealt with
continue;
if (kvp.Value == "??" && address.ContainsKey(kvp.Key)) // optional variable
{
address.SetMatched(kvp.Key);
}
else if (kvp.Value.StartsWith("?")) // variable takes any value
{
// matches a path element
address.SetMatched(kvp.Key); // this address element is matched
}
else // fixed value, has to be matched by item
{
bool matched = false;
if (address.ContainsKey(kvp.Key))
{
if (address.GetAsString(kvp.Key) == kvp.Value)
{
matched = true;
address.SetMatched(kvp.Key);
}
}
if (!matched)
return new List<RouteData>();
}
}
if (!address.FullyMatched) // Fails because one or more address elements could not have come from this route
return new List<RouteData>();
return rvds
.Select(r =>
{
var rd = new RouteData();
r.Do(kvp => rd.Values.Add(kvp.Key, kvp.Value));
rd.Route = route;
rd.RouteHandler = route.RouteHandler;
return rd;
})
.ToList();
}
示例2: GetUrls
/// <summary>
/// Get the possible urls that could have selected a given container or content item via a given route
/// </summary>
/// <param name="route">The route</param>
/// <param name="address">The content address</param>
/// <returns>The urls that could have accessed the object via the route</returns>
public List<string> GetUrls(Route route, Address address)
{
// Start list of urls which could map to this item. Note, catchall items (starting *) are treated as normal ones
List<string> urls = new List<string> { route.Url.Replace("{*", "{") };
var keyOutputs = route.KeyOutputs();
List<string> replaceWiths = null;
// Action & controller
if (!(keyOutputs.ContainsKey("controller") && keyOutputs.ContainsKey("action")))
throw new Exception("Route " + route.Url + " fails to define controller and action");
if (keyOutputs["controller"].StartsWith("?"))
throw new Exception("Route " + route.Url + " is a data route which lacks but must have a specified controller");
if (!ContentTypeHierarchy.ControllerActions.ContainsKey(keyOutputs["controller"]))
return new List<string>(); // Controller doesn't exist therefore no urls
// copy list so we can change 'actions'
var actions = ContentTypeHierarchy.ControllerActions[keyOutputs["controller"]].ToList();
if (keyOutputs["action"].StartsWith("?"))
{
if (keyOutputs["action"].Length > 1 && actions.Contains(keyOutputs["action"].Substring(1).ToLower()))
actions.Add("??");
UrlX.PermuteReplace(urls, "{action}", actions);
}
else if (!actions.Contains(keyOutputs["action"]))
return new List<string>(); // Fixed action doesn't exist therefore no urls
// Route vars addressing the content item
foreach (var kvp in keyOutputs)
{
if (kvp.Key == "controller" || kvp.Key == "action") // already dealt with
continue;
if (kvp.Value.StartsWith("?") && kvp.Value != "??") // variable takes any value
{
if (address.ContainsKey(kvp.Key)) // its part of the content item address
{
// path element is the default value, so this url element is optional
replaceWiths = new List<string> { address.GetAsString(kvp.Key) };
if (kvp.Value.Length > 1 && address.GetAsString(kvp.Key) == kvp.Value.Substring(1))
replaceWiths.Add("??");
// matches a path element
address.SetMatched(kvp.Key); // this address element is matched
}
else
{
replaceWiths = new List<string> { "?" };// no match, so many urls mapped to this item
int nUrls = urls.Count;
if (kvp.Value.Length > 1) // there's a default value so the element is optional
replaceWiths.Add("??");
}
UrlX.PermuteReplace(urls, "{" + kvp.Key + "}", replaceWiths);
}
else if (kvp.Value == "??")
{
UrlX.PermuteReplace(urls, "{" + kvp.Key + "}", new List<string> { "??" }); // optional variable
}
else // fixed value, has to be matched by item
{
bool matched = false;
if (address.ContainsKey(kvp.Key))
{
if (address.GetAsString(kvp.Key) == kvp.Value)
{
matched = true;
address.SetMatched(kvp.Key);
}
}
if (!matched)
return new List<string>();
}
}
if (!address.FullyMatched) // Fails because one or more address elements could not have come from this route
return new List<string>();
// Deal with possible url variations with omitted path elements
for (int i = 0; i < urls.Count; i++)
{
while (urls[i].EndsWith("/??"))
urls[i] = urls[i].Substring(0, urls[i].Length - 3);
if (urls[i].Contains("??"))
urls[i] = null;
}
return urls.Where(u => u != null).OrderBy(u => u.Split('/').Length).Select(u => "/" + u).ToList();
}
示例3: SaveWithRedirect
/// <summary>
/// Save the edited data to the api, with a check to see whether the data implies what url it exists on
/// and whether this has changed, in which case redirect to the new url
/// </summary>
/// <param name="data">Updated/created data to save</param>
/// <returns>Null or the url if we need to redirect</returns>
protected string SaveWithRedirect(object data)
{
RouteData rdOrig = RouteData.GetOriginal();
string redirectUrl = null;
// If address implied by address-mapped fields has changed, navigate to new address where item is now found
var currAddress = new Address(data.GetType(), rdOrig);
var newAddress = new Address(data);
if (currAddress.Any(kvp => newAddress.ContainsKey(kvp.Key)
&& newAddress[kvp.Key].ToString() != currAddress.GetAsString(kvp.Key)))
{
redirectUrl = ContentMap.Instance.GetUrl(data);
EventHub.Instance.ProcessEvent("Content.Move", this, Tuple.Create(rdOrig, data));
}
try
{
var create = GetIfCreate();
if ((create ?? false) && ContentMap.Instance.AddressOccupied(currAddress))
throw new LyniconUpdateException("There is an item already at this url");
Collator.Instance.Set(currAddress, data, create);
}
catch (LyniconUpdateException lux)
{
ModelState.AddModelError("updateFail", lux.Message);
}
return redirectUrl;
}