benfoster / fabrik.common Goto Github PK
View Code? Open in Web Editor NEWUseful stuff from fabrik
License: MIT License
Useful stuff from fabrik
License: MIT License
i'm having the issue in the above line as
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON
object (e.g. {"name":"value"}) into type 'System.Collections.Generic.IEnumerable
`1[ConsoleApplication1.contact]' because the type requires a JSON array (e.g. [1
,2,3]) to deserialize correctly.
give me a solution for this issue..
Makes it easy to generate or manipulate querystrings.
The Sitemap genereator may still generate a sitemap with utf-16 encoding specified in the xml declaration because it uses XmlTextWriter to generate the xml which retrieves the encoding from the environment settings and will overwrite the content encoding declared in the XDocument.
I had to create a custom ActionResult using StringWriter rather than XmlTextWriter:
public class Utf8StringWriter : StringWriter
{
public override Encoding Encoding
{
get { return new UTF8Encoding(false); }
}
}
public class Utf8SitemapResult : ActionResult
{
private readonly IEnumerable<ISitemapItem> items;
private readonly ISitemapGenerator generator;
public Utf8SitemapResult(IEnumerable<ISitemapItem> items) : this(items, new SitemapGenerator())
{
}
public Utf8SitemapResult(IEnumerable<ISitemapItem> items, ISitemapGenerator generator)
{
Ensure.Argument.NotNull(items, "items");
Ensure.Argument.NotNull(generator, "generator");
this.items = items;
this.generator = generator;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.HttpContext.Response;
response.ContentType = "text/xml";
response.ContentEncoding = Encoding.UTF8;
using (var writer = new Utf8StringWriter())
{
var doc = generator.GenerateSiteMap(items);
doc.Save(writer, SaveOptions.None);
response.Write(writer);
}
}
}
A MediatypeViewResultFormatter
with supported media type text/html
should handle text/html;q=0.7
.
Ideally we would parse the quality value but as a minimum should change the matching logic to use string.contains:
var supportedMediaType = SupportedMediaTypes.Any(
mt => acceptTypes.Any(at => at.Contains(mt, StringComparison.OrdinalIgnoreCase)));
We also need to provide a default formatter or at least return a 415 (unsupported media type) if none of the formatters are matched.
public static bool In<T>(this T t, params T[] values)
{
return values.Contains(t);
}
public static bool NotIn<T>(this T t, params T[] values)
{
return !In(t, values);
}
When the enum is nullable it should allow setting of the empty item text.
Hi,
Thanks very much for this, very good library. However, I'm having troubles with the build. It errors on this line:
var urlHelper = response.RequestMessage.GetUrlHelper();
In the PostResponseEnricher class. Any ideas?
I've started using CheckBoxListFor and it's a life saver!
Today, I was searching for a way to apply localization. In specific, I am looking to localize enum values.
Is there a built-in way to do this with EnumCheckBoxListFor?
For example, given that I have the following enum:
public enum SomethingEnum
{
[Display(Name = "Een")] // Dutch, but I want to introduce English and Dutch as localization options.
ONE,
[Display(Name = "Twee")]
TWO,
[Display(Name = "Drie")]
THREE
}
I've tried in two ways:
@Html.EnumCheckBoxListFor(model => model.SelectableSomethingEnums)
and
@Html.CheckBoxListFor(model => model.SelectedSomething, new SelectList(Model.SelectableSomethings, "Id", "Type"))
My ViewModel is something like this:
public class ViewModel
{
// For use with CheckBoxListFor
public IEnumerable<Something> SelectableSomethings { get; set; }
public int SelectedSomething{ get; set; }
// For use with EnumCheckBoxListFor
public IEnumerable<SomethingEnum> SelectableSomethingEnums{ get; set; }
}
public class Something
{
internal Something() { }
public int Id { get; internal set; }
public SomethingEnum Type { get; internal set; }
}
Is localization of enums currently supported? If so, in what way? If not, will it be added? :)
When the enum is nullable, the helper should add a blank entry into the select list.
Please?
It looks like the Nuspec files are there... Don't you just need to add it to the main package feed?
Currently httpResponse
is set after Enrich
is called which is a bit pointless since that's when we need access to the httpResponse.Request
.
As per Web API
Currently it can't be used with NH due to it requiring all methods to be marked virtual.
Should allow passing of serializer settings so we can, for example, return camelcased JSON.
public static MvcHtmlString EnumCheckBoxListFor<TModel, TEnum>(this HtmlHelper<TModel> html, Expression<Func<TModel, IEnumerable<TEnum>>> expression)
{
Ensure.Argument.Is(typeof(TEnum).IsEnum, "Must be a valid Enum type.");
var metadata = ModelMetadata.FromLambdaExpression(expression, html.ViewData);
var values = from v in Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
select new
{
Text = v.ToString().SeparatePascalCase(),
Value = v.ToString()
};
var selectList = new SelectList(values, "Value", "Text");
return html.CheckBoxListFor(expression, selectList);
}
Helpers such as Html.CheckBoxListFor
currently use ExpressionHelper.GetExpressionText(expression)
to generate element names. This generates an incorrect name when using the helper within nested editor templates. E.g.
Index.cshtml:
@Html.EditorFor(model => model.Other)
EditorTemplates/Other.cshtml
@Html.CheckBoxListFor(model => model.Tags, Model.AvailableTags)
Should generate the field name Other.Tags
but is actually just Tags
.
The solution is to use TemplateInfo
to get the full name:
var name = ExpressionHelper.GetExpressionText(expression);
var fullName = helper.ViewData.TemplateInfo.GetFullHtmlFieldName(name);
Update with the following:
public static IDictionary<string, string[]> ToSerializableDictionary(this ModelStateDictionary modelState)
{
return modelState.Where(x => x.Value.Errors.Any()).ToDictionary(
kvp => kvp.Key,
kvp => kvp.Value.Errors.Select(e => e.ErrorMessage).ToArray()
);
}
New in MVC 5 (I think)
I've tried to use BZip2OutputStream from Ionic.BZip2 (DotNetZip) to implement my own compressor and I failed. I've ended up with output stream contains only 4 bytes and then the error occurs on client when receiving the answer.
I've implemented compressor as direct implementation of ICompressor interface, here's the method for compression:
public async Task Compress(Stream source, Stream compressed)
{
using (BZip2OutputStream bzip = new BZip2OutputStream(compressed, true))
{
await source.CopyToAsync(bzip);
}
}
BTW Ionic.Zlib.GZipStream works well instead of .NET GZipStream.
The current implementation is incorrect, when dealing with Q values for the Accept-Encoding
header.
For example, with this request...
GET /foo HTTP/1.1
Accept-Encoding: gzip;q=0
...GZip should NEVER be chosen. And this one...
GET /foo HTTP/1.1
Accept-Encoding: gzip;q=0.5, deflate;q=1
... should choose deflate.
See: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
Fortunately, this should be an easy fix (since your code is very well built); just replace lines 28-30 of CompressionHandler.cs with something like this:
var compressor = (from a in request.Headers.AcceptEncoding
let q = a.Quality ?? 1.0
where q > 0
join c in Compressors on a.Value.ToUpperInvariant() equals c.EncodingType.ToUpperInvariant()
orderby q descending
select c).FirstOrDefault();
Now, this still doesn't get it to 100%, because identity
is a value that is allowed, which means no compression. I'm not sure what the best way to special-case that is, but I leave that as an exercise for the real developers. ;)
Currently return either the default view name or the default view name with prefix.
In some cases it would be useful for the client to specify the view to render.
return input.CopyToAsync(output)
.ContinueWith(task => output.Position = 0); // reset stream position back to 0
This line throws an exception because Position is not writeable (it's of type BufferManagerOutputStream
Does this even work?
There seems to be a bug in CheckBoxListFor concerning the values used after postback.
I cant get my posted values persist on the checkboxes, and I think it is because of the following inconsistency.
For the record, I have applied the fix from #30
The viewbag holds my data for the checkboxes (mind the selected = true!):
ViewBag.Sectors = sectors.Select(sector =>
new SelectListItem
{
Text = sector.Name,
Value = sector.Id.ToString(),
Selected = true
})
.ToList();
And the helper is using these values ...
@Html.CheckBoxListFor(model => model.SiteNewsSectors, ViewBag.Sectors as IEnumerable<SelectListItem>, new { @class = "form-control" })
... to render everything as expected.
However if I do a submit, and my model is not validated the page is redisplayed, but this time without checked boxes while the list is populated the same way, but for some reason the 'items' parameter has a list without the selected properties set to true.
Another submit reverses this and everything is checked again... If selected is set to false the check boxes are always unchecked as expected. But I cant populate the list with some values set to true that way either (my original problem).
Add HijaxAttribute and JsonStatusCodeResult currently in Fabrik.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.