mercredi 21 juillet 2010

User/role extension methods in an ASP.NET MVC view

This is a small Html helper that I use when I need to check the role in a view

public static class PageHelper
{
public static bool IsAdmin(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Admin");
}

public static bool IsCustomer(this ViewUserControl pg)
{
return pg.Page.User.IsInRole("Customer");
}
}
Don't forget to add the namespace for your extension methods in your Web.config :
<namespaces>
<add namespace="MyProject.Web.Helpers" />
</namespaces>

samedi 3 juillet 2010

Blogger open link in new window

In quest for the perfect setup for my blog, I know want to have my links open in a new browser window. The simplest way to do it is to add this tag in the <head></head> part of your template :
<base target="_blank" />

vendredi 2 juillet 2010

Source code syntax highlighting in blogger

I finally figured out how to have syntax highlighting for the source code in blogger thanks to this blog post.
This is the list of brushes I use :
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushBash.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushSql.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCSharp.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushRuby.js' type='text/javascript'/>
<script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'/>

AvalonEdit in a WPF application with MVVM

According to this forum message, it's not possible to bind the Text property from the Avalon Editor to a simple property of a ViewModel.
Instead, you can use the Document property from the editor and bind it to a property of your ViewModel.
Here is the code for the view :
<Window x:Class="AvalonEditIntegration.UI.View"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:AvalonEdit="clr-namespace:ICSharpCode.AvalonEdit;assembly=ICSharpCode.AvalonEdit"
        Title="Window1"
        WindowStartupLocation="CenterScreen"
        Width="500"
        Height="500">
    <DockPanel>
        <Button Content="Show code"
                Command="{Binding ShowCode}"
                Height="50"
                DockPanel.Dock="Bottom" />
        <AvalonEdit:TextEditor ShowLineNumbers="True"
                               Document="{Binding Path=Document}"
                               FontFamily="Consolas"
                               FontSize="10pt" />
    </DockPanel>
</Window>

And the code for the ViewModel :
namespace AvalonEditIntegration.UI
{
    using System.Windows;
    using System.Windows.Input;
    using ICSharpCode.AvalonEdit.Document;

    public class ViewModel
    {
        public ViewModel()
        {
            ShowCode = new DelegatingCommand(Show);
            Document = new TextDocument();
        }

        public ICommand ShowCode { get; private set; }
        public TextDocument Document { get; set; }

        private void Show()
        {
            MessageBox.Show(Document.Text);
        }
    }
}