Wednesday, December 15, 2010

"using" directive in aspx's pages.

It is funny but I don't know about this. I used full name of type (namespace+type name) in aspx pages. It is unnecessary. ASP.NET has directive for this.
<%@ Import namespace="value" %>
Sometime we omit in haste important things.

UPD: And more easy for Razor view engine:
@using MyNamespace

Monday, November 15, 2010

ASP.NET MVC aplication inside in an iframe (fix issue with empty session).

I developed application using ASP.NET MVC framework. It worked well but when I put my app inside of iframe I founded that in some case in Internet Explorer application missed session.
I started to investigate for the source of the issue.
I founded that application worked well if I decrease security level in the IE. But this fix weren’t suitable for me (I can`t request something in the customers). So I carried on with investigation.
Well description what happen I founded on the follow page (unfortunately, I didn’t found English version of this page, so I used google translate for figure out this article).
So for fix this I should add “PSP” header to response. How can I do it for the ASP.NET MVC application? In classic ASP.NET it is easy to do in code behind or in aspx file. I overrode ExecuteResult method in all classes inherited from ActionResult that used in my application. ViewResult for example:
public class CustomViewResult : ViewResult
{
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.AddHeader("P3P", "CP=\"CAO PSA OUR\"");
base.ExecuteResult(context);
}
}

After it I used my customized classes in base controller of all controllers in application (It was very nice idea to implement one base controller for all other controllers, wasn't it?). For example:
public class BaseController : Controller
{
protected override ViewResult View(string viewName, string masterName, object model)
{
if (model != null)
{
base.ViewData.Model = model;
}

return new CustomViewResult
{
ViewName = viewName,
MasterName = masterName,
ViewData = base.ViewData,
TempData = base.TempData
};
}
}

For figure out which methods I need to override I used cool tools - .NET Reflector.

Thursday, September 16, 2010

Unit Tests for ASP.NET MVC application that uses resources in code behind.

Download source files - here.

Some time ago I start to play with ASP.NET MVC. It is very well pattern and Microsoft made nice choose that implemented it for ASP.NET in my opinion.
I started a project that uses ASP.NET MVC. This project requests multilingual support. I use resources for this. It works well when I use resource's properties in the View's layer but when I call resource's properties from code behind. It's something likes to next:
public ActionResult Index()
{
ViewData["Message"] = CommonResource.WELCOME;
return View();
}
It works but I pay attention that this code breaks my Unit Tests. My Unit Tests looks like:
[TestMethod]
public void Index_Test()
{
//// Arrange
var controller = new HomeController();

// Act
var result = controller.Index();

// Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
}
It happens because I run my code in other project and my ResourceManager is not filled (it equals to null). How can I fix it? A first idea that came to mind is "extract interface".. but it is very annoying because resource's class is auto-generate and I should update this code after each updating of resources.. I made some research and found additional way for extracting resources - HttpContext contains GetGlobalResourceObject method! It is well way because I can set mock for HttpContext and for any its methods or properties.

Note: GetGlobalResourceObject returns object but I use only strings for localization. So I create linq method for HttpContext that returns string if it presents:
public static string GetGlobalResource(
this HttpContextBase httpContext
, string classKey, string resourceKey)
{
var result = httpContext.GetGlobalResourceObject
(classKey, resourceKey);
return null != result
? result.ToString() : string.Empty;
}
Pay attention, MissingManifestResourceException is possible in this code if you pass incorrect key/keys!

So I change pieces of code in Controller's classes in the next manner:
public ActionResult Index()
{
ViewData[CommonStrings.MESSAGE] = HttpContext
.GetGlobalResource(CommonStrings
.COMMON_RESOURCE, CommonStrings.WELCOME);
return View();
}
After it I should change my Unit Test's methods in next manner:
[TestMethod]
public void Index_Test()
{
//// Arrange
var httpContextMock = MockRepository
.GenerateMock<HttpContextBase>();
httpContextMock.Stub
(x => x.GetGlobalResource(
CommonStrings.COMMON_RESOURCE, CommonStrings.WELCOME))
.Return(CommonStrings.WELCOME);

var controller = new HomeController();
var context = new ControllerContext
(httpContextMock, new RouteData(), controller);
controller.ControllerContext = context;

// Act
var result = controller.Index();

// Assert
Assert.IsInstanceOfType(result, typeof(ViewResult));
ViewDataDictionary viewData = ((ViewResult)result).ViewData;
Assert.AreEqual(CommonStrings.WELCOME
, viewData[CommonStrings.MESSAGE]);
}
Moreover, you can see that I added additional checking related to localization.

Wednesday, March 24, 2010

Trimmed TextBlock for Silverlight.

Download source files - here

Not always Silverlight TextBlock that contains long text has enough space for shows all this text. TextBlock clips text in this case - this behaviour isn't best way for this. Because I wait that TextBlock not only trims but also adds "..." to clipped text. WPF has brilliant property that helps to resolve this trouble - TextTrimming. I implemented this possibility for Silverlight and now want to present it for you. Furthermore I implemented ToolTip that shows trimmed text (only if text is trimmed!)
Enjoy!

UPD: I have just updated performance and behaviour when width defined zero.

Wednesday, February 24, 2010

Trips&Ticks: Easy way to navigate from XAML to class's definition.

Working with XAML code in VS2008 I have just found that I can easy jump to class's or property definition. If I press CTRL and click left mouse button on certain class or property in XAML I will be immediately switched to Object Browser (if it is class or property of .NET or external dll) or to class's or property definition in my code (if it is class or property from my sources).
Eugene Dolinskyy just pay my attention that "clear" VS2008 doesn't have this possibility. This feature is presented by ReSharper.

Monday, January 25, 2010

Zoom event for FlowDocumentPageViewer (corrected)

Download source files - here

When I wrote my article Zoom and page chaged events for FlowDocumentPageViewer I make mistake, you can read about it - there. However Martin offer solution for this problem. But I don't like this solution, overriding of OnPropertyChanged isn't best solution because this method is risen in many cases so any logic there is serious performance issue.
So how can we fix it in other way? We can find object of Slider in Visual Tree of FlowDocumentPageViewer and subscribe to ValueChanged event and call OnZoomChanged(); there.
Bingo! It fixes my trouble.

Sunday, January 24, 2010

Trips&Ticks: How to implement GroupName for RadioButton in WPF DataGrid's column?

Download source files - here

It is very easy. We should use DataGridTemplateColumn for this. My acquaintance requested to me show it. I added source code where implemented this.

Some notes:
  • You should turn off AutoGenerateColumns feature. In other case control will have duplicate values.
  • I recommend to turn off CanUserAddRows. In other case user can pick undefined row.
Enjoy Yulian ;).

Friday, January 15, 2010

Web access to Team Foundation Server.

I have been starting to work with Team Foundation Server as source control and bag tracking system for 2 month. In this time we migrated from one TFS to another. So sometime we needed access to both these servers (one as bug track, another as source control). I said to my manager about web access tool for TFS he heard it already that this tool is very cool but expensive.
It isn't truth. This tool is useful but full freeware! Because Microsoft Acquired TeamPlain 3 year ago :). When I tried to find it - I was a bit disappointed I couldn't found this tool - all links were broken. Little find out shows that TeamPlain is renamed to Team System Web Access. I founded article that describes How to: Install Team System Web Access (this page also contains broken link to installation of Team System Web Access :) ) and after all I found download page.
That isn't fresh news, but it is very upset that this useful tool is so hard for found.