This week in tweets (07-24-2010)

Photoshop your own Windows Phone 7 applications


It looks like Microsoft is not just making the Windows Phone 7 application development experience easy for developers, but it turns out designers have most of their job cut out for them. If you need to mock up an application UI for Windows Phone 7, it’s now as easy as mashing up some Photoshop files from the “Design Templates for Windows Phone 7″ resource. Since finding useful information on M...  read more


Windows Phone 7 in-depth preview

Source: Engadget

It's been a long road, hasn't it? Well, in some respects, it hasn't -- in fact, it's only been about two years since development of Windows Phone 7 as we know it today kicked off -- but when you consider that this product will be replacing Windows Mobile 6.5, that puts things in proper perspective. In fact, even the very ...  read more


Google signs 20-year deal to power data centers with wind energy

Source: Engadget

It's not the first investment Google has made in wind power, but anyone wondering about its commitment needn't look any further than the company's just-announced deal with NextEra Energy. It's agreed to buy wind power from NextEra's wind farm in Iowa for the next twenty years, which it says will provide enough power to supply "several" of its data centers. What's more, Google says that the size...  read more


Apple posts record $3.25b profit in first full quarter of iPad sales, says more 'amazing products' coming this year

Source: Engadget

Apple just posted up its third quarter earnings -- its first full quarter selling the iPad -- and, well, it's raining cash in Cupertino. The company posted a record profit of $3.25b on record revenues of $15.7b, which is up from $1.83b and $9.73b from a year ago. The big stat? Apple sold 3.27 million iPads, nearly matching the 3.47 million Macs sold -- and Mac sales were up 33 percent from a ye...  read more


Uploading a File (Or Files) With ASP.NET MVC


I wanted to confirm something about how to upload a file or set of files with ASP.NET MVC and the first search result for the phrase “uploading a file with asp.net mvc” is Scott Hanselman’s blog post...  read more


Apple TV Rumored To Get $.99 TV Show Rentals, But Will The iPad And iPhone Get The Service Too?

Source: TechCrunch

The Apple TV might suddenly be part of the next big thing from Apple. We’ve heard that there’s a major retooling underway and Apple shifted some of their best people on the project. Updated hardware is likely, but new services will likely be at the forefront of the relaunch. The latest rumor states that Apple is in contract talks to bring TV show rentals to the platform. They would probably wo...  read more



The articles listed in this post are random selections from my Google Buzz account and are not my work. You may need to obtain authorization from the owner by clicking the source for each article before using them.


Server side conversion of UserControl to html

In order to get the html out of an ASP.NET control you can use the following code:

string pathToUserControl = "~/Controls/MyUserControl.ascx";
    
System.Web.UI.Page page = new System.Web.UI.Page();
System.Web.UI.UserControl control = page.LoadControl(pathToUserControl);
page.Controls.Add(control);
    
StringWriter writer = new StringWriter();
HttpContext.Current.Server.Execute(page, writer, false);
    
string renderedHtml = writer.ToString();

This method is mostly used to send the html back to the client browser using web services.


Strongly typed view in ASP.NET MVC

When passing the model to MVC you have a couple of options.

Option 1: Passing un-typed model to the view

public ActionResult Product(string id)
{
    var product = new Product();
    product.ID = id;
    product.Name = "iPhone";
    ViewData["product"] = product;
}

In your view you have to cast the value in ViewData to the correct type as follows:

<%
    var item = (Product)ViewData["product"];
%>
<div>
    ID: <%= item.ID %>
    Name: <%= item.Name %>
</div>

Option 2: Pass a strongly typed model to the view

// ViewData["product"] = product;
ViewData.Model = product;

The Page directive of your view must specify the type it expects as follows:

<%@ Page Title="blah" ... Inherits="System.Web.Mvc.ViewPage<Product>" %>

Sometimes you may need to pass more than one object to the view. You can handle this by creating a new class that encapsulates all objects you need and pass an instance of that class to the view instead.


How to force SSL on your ASP.NET website?

I have seen may implementations of forcing SSL on websites that require changes to IIS and creating dummy aspx pages for the redirect, but nothing beats the simple and elegant code that follows:

Courtesy: IKOSoftware

// Force SSL
//
Uri currentUrl = System.Web.HttpContext.Request.Url;
if (!currentUrl.IsLoopback)
{
    if (!currentUrl.Scheme.Equals(Uri.UriSchemeHttps, 
                                  StringComparison.CurrentCultureIgnoreCase))
    {
        // Build the Secure Uri
        //
        UriBuilder secureUrlBuilder = new UriBuilder(currentUrl);
        secureUrlBuilder.Scheme = Uri.UriSchemeHttps;
    
        // Use the default port.
        //
        secureUrlBuilder.Port = -1;
    
        // Redirect and end the response
        //
        System.Web.HttpContext.Response.Redirect(secureUrlBuilder.Uri.ToString());
    }
}


We just got AJAXed

Woohoo! Microsoft has released the final version (1.0) of ASP.NET AJAX library. That means we can finally write production quality AJAX applications. Microsoft fully supports the library and is giving out the source code for the entire library for free. You can modify and re-distribute the source code at will for both commercial and non-commercial applications. You can read more about this on Scott Guthrie's blog. The AJAX library will be embedded into the next version of ASP.NET (3.0) scheduled to be released by this year end.

Update: I have updated this website to use ASP.NET AJAX 1.0 RTM build Cool

Thanks,


HTTP Compression

I serve my website off of my home computer which has a decent broadband connection. The upload speed however is a mere 384KB, which used to make my web pages slow in loading up until I enabled HTTP compression and let me tell you it is a real blessing. Not only does it save a lot of bandwidth, but more importantly my pages are a lot more responsive.

Most modern browsers support two compression algorithms, GZip and Deflate. I am using Blowery an open source compression software for serving my web pages in Deflate compression mode. The other thing I enabled last week on my site was AJAX using the new Microsoft Atlas Framework. What I saw was that one of the JavaScript files served to the browser was a whopping 242KB. Although script files get cached by the browser, the speed on the first serving on my web page would be aweful.

The Javascript files are served through a new concept in ASP.NET called Web Resources, where you can embed JavaScript and image files directly into your assembly as resources. When the ASP.NET engine gets a request for these resources it extracts these files out of the assembly and serves them. The file that does is called WebResource.axd. I don't have a clue as to what that file is / does, but I will do some reasearch to find out. The point is that by enabling Blowery the JavaScripts on my web pages stop working. I found a patch for this. You have to add the following lines to the httpCompress section required by blowery in the web.config

<excludedPaths> 
      <add path="ebresource.axd"/>
      <add path="webresource.axd"/> 
</excludedPaths>

Having fixed the web pages, I still wanted to do something about those huge Atlas files. Luckily some smart Atlas developers at Microsoft have coded a compression routine for the resource files that are served through webresource.axd. You can enable it by adding the following line in the httpModules section to your web.config.

<httpModules>
      <add name="WebResourceCompression"
           type="Microsoft.Web.Services.WebResourceCompressionModule" />
</httpModules>

An important point to note is that this needs to be above the blowery http module. That is blowery also is an httpModule that needs to be added to the httpModules section in the web.config. However by keeping the add tag for blowery above the atlas module, my Atlas JavaScripts were not getting compressed. Moving the tag fixed the problem.

Have fun!


Enable AJAX with Atlas

Start off by downloading and installing the latest CTP from the asp.net site. Watch the "Get Started with “Atlas”?" video on this page to install and setup Visual Studio to be Atlas ready. You can then watch the other videos that show how to use some neat tricks to avoid server postbacks.

For the view comments feature on my blog, I created the following:

  • A custom control (by extending System.Web.UI.WebControls.WebControl) that accepts a dataset as a property and loops through the dataset in the Render method to output the necessary html.
  • A webservice that talks to my business logic, gets the necessary data, passes it to the custom control and outputs whatever the control renders.

Let's look at a simple example:

HelloControl.cs

public class HelloControl : WebControl
{
      private string _text; 
      public string Text
      {
            get { return _text; }
            set { _text = value; }
      }
      protected override void Render(HtmlTextWriter writer)
      {
         writer.Write("<span style='font-weight:bold;color:#dd0000'>Hello "
                       + _text + "</span>");
      }
}

All this control does is given an input text, it outputs the text in bold and red.

HelloService.cs

class HelloService : WebService 
{ 
      [WebMethod] 
      public string SayHello(string name) 
      { 
            HelloControl c = new HelloControl(); 
            StringWriter writer = new StringWriter(); 
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer); 
            c.RenderControl(htmlWriter); 
            return writer.ToString(); 
      } 
}

That's it, you now have a webservice that spits out html which can then be used to fill certain sections of your web page. You can watch the "“Atlas” Enable an Existing Web Service?" video to see how to use webservices on the client side using Atlas.

Have fun!


Atlas

I have been playing around with atlas for the past couple of days. For those who don't know about atlas please visit here. It is a new AJAX framework that sits on top of ASP.NET and let's you enable AJAX on any ASP.NET control.

Today I added two features to the site using Atlas.

  • Ability to view comments without going to the feedback page
  • Ability to navigate the calendar without page refresh

UpdatePanel in Atlas is an awesome control. In order to enable AJAX on the Calendar control all I had to do was to add the UpdatePanel control on the ASP.NET page and move my existing Calendar control inside it. That's it, I now have a calendar control that can be navigated without a page refresh.

I will explain the code behind the view comments feature in another post.


Component Art web controls

AJAX has been the buzzword of the industry for quite some time now, but coding in it has always been difficult. Writing code in JavaScript is not all that difficult but debugging the code is what's the most difficult part of AJAX. The second difficult part is training. I was looking at different components out there for ASP.NET when I came across Component Art.

I have seen Component Art ads in the MSDN magazine many times, but never paid much attention to it, until today. I decided to try it out and was completely blown away. If you ever have thought of doing AJAX and have been waiting for a tool, Component Art should definitely be on your short list. I have to admit I have not tried other tools out there, but I did check Infragistics, their live demos are not as polished as Component Art but I will still give it a try in a few days. Both companies pack 6 to 8 controls in their package and charge around $700. I think these tools are a bit pricy, but then you have to decide whether you want to be a rocket scientist or use the tools and move on. Bottom line, I think the controls are worth every penny given the kind of neat stuff you can do with it.


ASP.NET 2.0 Site Administration

For the last couple of nights I have been coding the admin module for the blogs using the Site Administration feature of ASP.NET, there was very little coding required to build an entire password protected site using Visual Studio 2005. Everything is drag and drop and few clicks here and there. It simply rocks!!!

Here's an article that explains the concept and takes you step by step on building a password protected website.

https://www.sitepoint.com/article/asp-net-2-security