gpt4 book ai didi

asp.net-mvc - MVC RouteUrl 不尊重 Exclude 属性

转载 作者:行者123 更新时间:2023-12-04 06:22:19 25 4
gpt4 key购买 nike

(我使用的是 ASP.NET MVC 2。)

使用 Url.RouteUrl(object) 时如何排除某些属性进入查询字符串?

具体来说,在传递给我的 View 的模型对象中,我有一个字符串数组(技术上是 IEnumerable ),我想排除它。

我认为带有 Exclude 的 Bind 属性应该这样做,但它不起作用。我试过把 [Bind(Exclude = "Sizes")]在我的课上,但我不断收到如下所示的 URL:

http://localhost/?Sizes=System.String[]

最佳答案

扩展方法和反射来救援!

/// <summary>
/// Add UrlHelper extension methods that construct outgoing URL's but
/// remove route values that are excluded by the Bind attribute's
/// Include or Exclude. The methods to mirror are those that take an
/// object as an argument:
///
/// public string Action(string actionName, object routeValues);
/// public string Action(string actionName, string controllerName
/// , object routeValues);
/// public string Action(string actionName, string controllerName
/// , object routeValues, string protocol);
///
/// public string RouteUrl(object routeValues);
/// public string RouteUrl(string routeName, object routeValues);
/// public string RouteUrl(string routeName, object routeValues
/// , string protocol);
/// </summary>
public static class UrlHelperExtensions
{
public static string Action(this UrlHelper helper, string actionName
, object routeValues, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);

if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}

// Internally, MVC calls an overload of GenerateUrl with
// hard-coded defaults. Since we shouldn't know what these
// defaults are, we call the non-extension equivalents.
return helper.Action(actionName, routeValues);
}

public static string Action(this UrlHelper helper, string actionName
, string controllerName, object routeValues
, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);

if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}

return helper.Action(actionName, controllerName, finalRouteValues);
}

public static string Action(this UrlHelper helper, string actionName
, string controllerName, object routeValues
, string protocol, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);

if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}

return helper.Action(actionName, controllerName
, finalRouteValues, protocol);
}

public static string RouteUrl(this UrlHelper helper, object routeValues
, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);
if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}
return helper.RouteUrl(finalRouteValues);
}

public static string RouteUrl(this UrlHelper helper, string routeName
, object routeValues, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);
if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}
return helper.RouteUrl(routeName, finalRouteValues);
}

public static string RouteUrl(this UrlHelper helper, string routeName
, object routeValues, string protocol
, bool onlyBoundValues)
{
RouteValueDictionary finalRouteValues =
new RouteValueDictionary(routeValues);

if (onlyBoundValues)
{
RemoveUnboundValues(finalRouteValues, routeValues);
}

return helper.RouteUrl(routeName, finalRouteValues, protocol);
}

/// <summary>
/// Reflect into the routeValueObject and remove any keys from
/// routeValues that are not bound by the Bind attribute
/// </summary>
private static void RemoveUnboundValues(RouteValueDictionary routeValues
, object source)
{
if (source == null)
{
return;
}

var type = source.GetType();

BindAttribute b = null;

foreach (var attribute in type.GetCustomAttributes(true))
{
if (attribute is BindAttribute)
{
b = (BindAttribute)attribute;
break;
}
}

if (b == null)
{
return;
}

foreach (var property in type.GetProperties())
{
var propertyName = property.Name;
if (!b.IsPropertyAllowed(propertyName))
{
routeValues.Remove(propertyName);
}
}
}
}

关于asp.net-mvc - MVC RouteUrl 不尊重 Exclude 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6400838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com