moddev.net
域名年龄: 24年3个月9天HTTP/1.1 302 Found 连接:关闭 其他指令:不缓存 缓存控制:不缓存 目标网址:/ HTTP/1.1 301 永久重定向 缓存控制:max-age=900 类型:text/html 目标网址:http://www.nathan-taylor.net 服务器:Microsoft-IIS/7.5 ASP.NET版本:4.0.30319 语言环境:ASP.NET 访问时间:2015年01月09日 18:19:31 文件大小:0 文件时间(秒):1 连接:keep-alive HTTP/1.1 302 Redirect 类型:text/html; charset=UTF-8 目标网址:http://blog.nathan-taylor.net 服务器:Microsoft-IIS/7.0 语言环境:ASP.NET 访问时间:2015年01月09日 18:19:31 文件大小:153 HTTP/1.1 304 Not Modified 过期时间:2015年01月09日 18:19:33 访问时间:2015年01月09日 18:19:31 缓存控制:private, max-age=0 网页标记:"6f186192-3915-43f5-ac40-7a8c01de9c78" 服务器:GSE Alternate-Protocol: 80:quic,p=0.02,80:quic,p=0.02 网站编码:UTF-8
.NET Addict
A glimpse into the daily affairs of an ASP.NET MVC developer by Nathan Taylor.
Monday, May 7, 2012
C# Truncate String - Whole Words
This is an update to my original string truncate helper which can be set to respect whitespace when terminating a string. This version will start at the character index specified as max length and then iterate back to the first instance of whitespace (if it exists), thus keeping whole words intact.public static string Truncate(this string str, int maxLength, bool onlyOnWhiteSpace = false, bool appendEllipses = false) {
if (str == null || maxLength >= str.Length)
{
return str;
}
if (onlyOnWhiteSpace
&& !char.IsWhiteSpace(str[maxLength])
&& str.Substring(0, maxLength).Any(char.IsWhiteSpace))
{
for (; maxLength >= 0; maxLength--)
{
if (char.IsWhiteSpace(str[maxLength]))
{
break;
}
}
}
return str.Substring(0, maxLength) + (appendEllipses ? "..." : "");
}
Posted by
Nathan Taylor
at
1:59 PM
0
comments
Share
|
Labels:
c#,
string,
string manipulation,
substring,
truncate
Tuesday, November 22, 2011
ASP.NET MVC Comma Separated Values Model Binder
In some scenarios such as data filtering it becomes necessary to pass multiple values to a single collection-typed request parameter. In typical flows this is achieved by passing multiple pairs of key and value parameters to the request.
/path/to/foo?key=value1&key=value2&key=value3
While this works well, sometimes it is desirable to have something that is more human-readable. In an effort to satisfy that requirement, I have built a custom model binder to handle passing multiple values to a single parameter in the form of a single comma separated value.
/path/to/foo?key=value1,value2,value3
This CommaSeparatedValuesModelBinder supports both generic collections and standard array types so long as the underlying type of the collection inherits from IConvertible (int, string, etc). In addition to supporting a comma separated value assignment, the implementation also maintains support for the default format of multiple key and value pairs.
Here's the code:
public class CommaSeparatedValuesModelBinder : DefaultModelBinder
{
private static readonly MethodInfo ToArrayMethod = typeof(Enumerable).GetMethod("ToArray");
protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
{
if (propertyDescriptor.PropertyType.GetInterface(typeof (IEnumerable).Name) != null)
{
var actualValue = bindingContext.ValueProvider.GetValue(propertyDescriptor.Name);
if (actualValue != null && !String.IsNullOrWhiteSpace(actualValue.AttemptedValue) &&
actualValue.AttemptedValue.Contains(","))
{
var valueType = propertyDescriptor.PropertyType.GetElementType() ??
propertyDescriptor.PropertyType.GetGenericArguments().FirstOrDefault();
if (valueType != null && valueType.GetInterface(typeof (IConvertible).N
© 2010 - 2020 网站综合信息查询 同IP网站查询 相关类似网站查询 网站备案查询网站地图 最新查询 最近更新 优秀网站 热门网站 全部网站 同IP查询 备案查询
2025-12-22 07:58, Process in 0.0097 second.