blog.mha.dk
The on-line blog of Michael Holm Andersen

ASPX Edit Helper Add-In For Visual Studio

Thursday, 11 February 2010 17:07 by mha

Jacob mentioned a nice little free Add-In for VS2005/2008 yesterday while we we’re chatting on Messenger :-)

Short description: “The ASPX Edit Helper is an add-in for Visual Studio 2005 for those who like to type ASPX markup themselves instead of using the visual designer"

Usage:

Type <asp:Label and press Enter. The add-in will replace it with:

<asp:Label runat=”server” id=”asp_Label2943″ />

The add-in will also highlight the ID attribute value so you can immediately type a new ID for the control.

Very handy, thanks to Jacob for mentioning this – free download at: http://www.ardentdev.com/aspxedithelper

Categories:   ASP.NET | Tools
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

ASP.NET MVC Best Practices

Wednesday, 10 February 2010 16:49 by mha

Kazi Manzur Rashid has put together a blog post (in 2 parts) about ASP.NET MVC Best Practices which is worth reading if you’re working with ASP.NET MVC.

ASP.NET MVC Best Practices (Part 1)
ASP.NET MVC Best Practices (Part 2)

Categories:   ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

ASP.NET MVC and ASCX

Tuesday, 9 February 2010 23:51 by mha

Jeffrey Palermo has an inspiring article about templating and (why not) to use .ascx aka UserControls in ASP.NET MVC – I’ll definitely be using this approach:

http://jeffreypalermo.com/blog/asp-net-mvc-and-the-templated-partial-view-death-to-ascx/

jQuery IntelliSense on ASP.NET MVC

Tuesday, 9 February 2010 21:51 by mha

If you’re using ASP.NET MVC, you’re probably also using jQuery and hence would like to have IntelliSense support in your ViewPages. According to Scott Gu all you need to do is to make sure you’re having the “-vsdoc.js” file in your script folder (e.g. “jquery-1.3.2-vsdoc.js”) and VS2008 (with the patch installed!) will find this file and jQuery IntelliSense works – however as it turns out if you’re using a path which starts with “/” (as you should!) VS2008 is unable locate the “-vsdoc.js” file and you will not have any jQuery IntelliSense support.

So there’s a little more to know about how exactly jQuery IntelliSense actually works in order to make this work correctly (not to mention IntelliSense support in .ascx files). Luckily Jouni Heikniemi have written a great article about this – check it out:

http://www.heikniemi.net/hardcoded/2009/08/jquery-intellisense-on-asp-net-mvc/

TamperData – security test your web applications

Saturday, 6 February 2010 02:26 by mha

Working more and more with ASP.NET MVC I’ve found the Tamper Data Firefox Add-on to be quite useful. With this add-on you can view HTTP/HTTPS headers and browser-requests and (which is really awesome) edit POST parameters.

Use this add-on to security test your web applications by modifying POST parameters and simulate a scenario where a user changes/tampers with information sent to your site, like e-commerce sites where a user tries to buy cheap stuff, hack passwords, etc..

LINQ and PagedList

Wednesday, 27 January 2010 03:04 by mha

If you’re using LINQ you probably know about the .Skip and .Take methods. However often you need to do paging, hence you probably use something similar to this to get around that:

public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
    {
        PageIndex = pageIndex;
        PageSize = pageSize;
        TotalCount = source.Count();
        TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

        this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
    }


However you don’t need the above code – instead take a look at the CodePlex project called PagedList located at http://pagedlist.codeplex.com/

PagedList allows you to take any List<T> and by specifying the page size and desired page index, select only a subset of that list, there’s also a StaticPagedList if you should need that.

Simply download the source, run “Release.bat” to build a release version and reference the .dll file in your solution, then you can have code like this:

var somePage = list.ToPagedList(4, 50); // fifth page, page size = 50

Using Fiddler with the ASP.NET Development server

Tuesday, 19 January 2010 18:28 by mha

Fiddler does not by default work together with the ASP.NET development server, however you can easily fix this.

In Fiddler press Ctrl+R (Rules) and go to the “OnBeforeRequest” method, here you add a single line:

oSession.host = oSession.host.replace("mhanotebook","127.0.0.1");

The “mhanotebook” is the name of your computer. The problem is that Fiddler works as a proxy and change this settings in IE7 to forward request to fiddler. however IE7 does not forward calls if the url contains localhost.

After applying the above rule, you can now do a request like this: http://mhanotebook:50016/product and Fiddler will “kick in”, nice :-)

Using LINQPad to query the database

Tuesday, 19 January 2010 03:17 by mha

If you’re working with LINQ, you simply can’t live without LINQPad. Below is a screenshot from the query:

from ans in UserAnswers
group ans by new {ans.AnswerId, ans.Answer.Content, 
                  Q=ans.Answer.Question.Content}
into myGroup
select new {myGroup.Key.Q, myGroup.Key.Content, Antal = myGroup.Count()}
 

- being able to do realtime queries against the database is simply awesome. And for only $29 you’ll have Autocompletion (which of course is a must-have / timesaver).

The query shows simple statistics about a poll, where question (Q), answer (Content) and total votes (Antal) is pulled out from the database – I’ve blurred the value of Count() in order to “protect” the clients data…

limfjordspoll_linq

Categories:   LINQ | Tools
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

LINQ Projection and SelectListItem

Monday, 18 January 2010 00:16 by mha

A small example of where LINQ Projection might come in handy is when you’re working with MVC. In the below example LINQ Projection is used to transform a enumeration of EmployeeCategory instances into an enumeration of SelectListItem instances:

var rep = new mhaRepository();
var person = rep.GetPersonByID(id);
ViewData["EmployeeCategories"] = from c in rep.EmployeeCategories
                         select new SelectListItem
                         {
                             Text = c.Name,
                             Value = c.ID.ToString(),
                             Selected = (c.ID == person.CategoryID)
                         };

In the View all that is needed to render the DropDownList is this:

<%= Html.DropDownList("EmployeeCategories") %>

Categories:   LINQ | ASP.NET MVC
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Sidebar translation gadget

Tuesday, 12 January 2010 03:54 by mha

I’ve been looking for a translation gadget and today I (finally) found a good one:

translategadget

It uses Google as the “engine “, supports 43 languages and is free:

http://www.photo-bon.com/en/translator_en.htm