Profiling ASP.NET website in Visual Studio with Performance Wizard

Those who use Visual Studio 2010 may know that it now ships with a performance analysis tool that instruments your application in various ways.

You can run from Analyze > Launch Performance Wizard

Launch_perf

 

 

 

 

If you are profiling an ASP.NET website project then you must open it from File > Open Website > Local IIS

local_iis

 

Though there is one problem, if you do not have the right options enabled in Windows you might get an exception that reads

Error
VSP 7008: ASP.net exception: "The website metabase contains unexpected information or you do not have permission to access the metabase.  You must be a member of the Administrators group on the local computer to access the IIS metabase. Therefore, you cannot create or open a local IIS Web site.  If you have Read, Write, and Modify Permissions for the folder where the files are located, you can create a file system web site that points to the folder in order to proceed."

On this Visual Studio Profiler team blog post I found the right options that I needed to enable.

To check this in Windows 7:

  1. Open ‘Control Panel\Programs\Programs and Features’ (or run ‘appwiz.cpl’).
  2. Choose ‘Turn Windows features on or off’.
  3. In the ‘Internet Information Services’ section, make sure that the following options are selected.

After selecting the options re-launch the profiler, Visual Studio builds the solution, launches a browser window, browse some of the pages of your website while Visual studio records in the background, close the browser or hit Stop in visual studio to get the report.

Hope it helps you, let me know.

New features and improvements in ASP.NET vNext

I watched Damien Edward’s excellent presentation and learned about the new features that will be available to us in the vNext of ASP.NET Webforms, if you like to get the full details I encourage you to watch the video. Here I have summarized the main features.

Strongly typed Data control and Model Binding support

Using these you will be able to bind strongly typed models to your datacontrols and get full intellisense for them.

Image(31)[5]

Image(1)[5]

You can also tell the controls to get their data from a method


Image(32)[4]

In your codebehind file you define your method like this, this works great with Entity Framework 4.1

Image(3)[4]

Add Querystring value with method parameter

Another improvement that is coming up is the support of attributes in method arguments as in the following code.

It is looking under the querystring collection for the key minProductsCount, converting it to an integer if found or leaving null because it is nullable, saving a lot lines of code that we write today.

Image(4)[4]

Image(5)[4]

Take the above value from a control and other collections

There will be support for other attributes as well, suppose you want to get the value from a control of the same name, a Form Collection, Cookie, ViewState even from your custom attribute and so on

Image(6)[4] Image(7)[4]
Image(8)[4] Image(9)[4]
Image(10)[4]  

Change parameter id

By default it uses the parameter name but if your control’s id is different then your parameter name, you can specify the id like this

Image(11)[4]

Master/Detail scenario in GridView

Have another grid view bound to the master gridview, define the select method on the detail gridview, then tell the method to pull the selected id from the master gridview

Image(15)[4]

Image(16)[4]

The results look like this

Image(17)[4]

Update support

Image(18)[4]

Image(33)[4]

Because there is no info of the Product from the gridview due to the databinding expressions, the Products property is null so instead of binding Product info to the gridview and getting it back on postback, we’ll have the following method

TryUpdateModel(category)

Now the product collection still exists, just update this model with the info from the gridview and save

Image(20)[4]

Adding Validation using Data Annotations

Image(21)[4] Image(22)[4]
Save only if the model is in valid state or reload the category

Image(23)[4]

Adding custom model exception on Db.SaveChanges();

For example if you have a unique key constraint on your table then you will push the exception from Entity Framework into the Model state to be able to show the error on page in gridview, like this.

Image(24)[4]

Image(25)[4]

Support to bind the Gridview to Dynamic is coming up

Image(26)[4]

Async in WebForms vNext

New methods and keywords are coming up for improving asynchronous communications

Image(28)[4]

The call will return immediately and the results will be filled in later

Chaining three async method calls together then returning the results, using PageAsyncTask

Image(29)[4]

The result of those three files loaded looks like this

Image(30)[4]

There’s more

Unobtrusive built in validators – no inline javascript, AntiXSS Encoding, Html5 updates and runtime combination and minification of Javascript and Css files is coming up

Summary

  • Webform is not dead
  • Improved data binding via Strongly typed data controls and model binders
  • Easier Async programming coming to ASP.NET

..and much more

Microsoft Web Camp: My demo of MS Web Stack of Love

Last week I co-presented a session titled “Creating Cutting Edge Websites for Phones, Slates and Beyond” at Microsoft Web Camp in Dubai with Ronald Widha.

  • Opening Address, Michael Mansour
  • Development on Internet Explorer 9, Shaymaa Al-Terkait
  • Building an eCommerce Site in 13 Minutes with Web Matrix, Asli Bilgin
  • Creating Cutting Edge Websites for Phones, Slates and Beyond with WebMatrix and VS2010, Ronald Widha and Zubair Ahmed
  • Q&A with Pizza Dinner and drawing for a free Windows Phone 7!

From Microsoft Web Camp in Dubai

Ronald Widha talked about the challenge facing today’s designer and developers to build User interfaces that fit on desktop and phones alike, he discussed the Responsive UI design pattern and showed how Html5, CSS Media Queries and Fluid Images can help solve this issue to some extent. Get Ronald’s slides

I then showed some goodness from Microsoft Web Stack of Love and took Ron’s user interface markup to the new CSHTML page type in Web Matrix and changed it to get the content from a SQL Compact Edition database using the Razor view syntax, launched the same solution in Visual Studio from Web Matrix then added Entity Framework and WCF Data Services to the mix.

You can download all the tools using Web PI and get my code.

Here’s the video (I come at 23:20)

Creating Cutting Edge Website for Phone, Slate and Beyond from Ronald Widha on Vimeo.

Speaker(s): Ronald Widha, Zubair Ahmed

April 16, 2011

Microsoft, Dubai

The process of creating a website now always start with a question on figuring out the devices you want to support for; PCs, mobile phones and/or slate devices. Each of these devices may have different screen sizes, capabilities etc. For simplicity, designer/developer often chooses to build dedicated websites for each one of the platform which often leads to maintenance nightmare.

This talk showed how to design and develop a dynamic website for the various platforms using Responsive Design techniques. We’re starting up the process with WebMatrix and transitioning to Visual Studio 2010 as we progress. We touched on Html5, CSS3, Razor, Entity Framework 4 and C#.

Binding a Custom Meta field to a Template in Sitefinity with a ASP.NET Usercontrol

To get the background about this issue please read this article in Sitefinity knowledge base.

The last section of the article suggests that you can use the following syntax to set the NavigateUrl property of the ASP.NET Hyperlink control with its Text property

<asp:HyperLink ID="PDFAttachment" runat="server" NavigateUrl='<%=this.Text %>'></asp:HyperLink>  

 

However the above doesn’t work and <%= %> is rendered as plain text

After trying to figure this out for a while I found the following way to do this, the trick is to create a custom user control that implements ITextControl interface, like this

public partial class UserControls_PDFAttachmentDownload : System.Web.UI.UserControl, ITextControl

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    private string text;

 

    public string Text

    {

        get

        {

            return this.text;

        }

        set

        {

            if (!string.IsNullOrEmpty(value))

            {

                this.showdownload.NavigateUrl = ResolveUrl(value);

            }

        }

    }

}

The highlighted line above sets the NavigateUrl property from the Text value that is bound to the control.

The ASCX part of the user control simply has a ASP.NET Hyperlink control in it

<asp:HyperLink ID="showdownload" runat="server" Text="Download" />

To use this control just add a reference in the Template usercontrol that Sitefinity uses and your on your way.

<%@ Register TagName="Attachment" TagPrefix="PDF" Src="~/UserControls/PDFAttachmentDownload.ascx" %>

<div class="sf_singleNews">

....

    <p>

       <PDF:Attachment ID="PDFAttachment" runat="server" />

    </p>

....

</div>

Hope this little trick helped you, let me know.

Permanent Redirections with Response.PermanentRedirect Extension method

To redirect a page request to another page ASP.NET ships with Response.Redirect() since the v1 that takes a string Url as the input parameter.

You will recall that this is not a permanent redirect, you know that to be able to make a permanent redirect you will have to do something like this 

HttpContext.Current.Response.Status = "301 Moved Permanently";HttpContext.Current.Response.AddHeader("Location", StringUrl);

ASP.NET 4.0 now ships with a Response.PermanentRedirect() helper method that redirects the user permanently to a new page. Watch Joe’s quick hit video to see it in action.

Fortunately we don’t need to wait for .NET 4.0 to have a fancy method like above, we can with the help of C# Extension methods, create a helper method today.

For example take a look at this code

public static void PermanentRedirect(this HttpResponse response, string StringUrl) {     HttpContext.Current.Response.Status = "301 Moved Permanently";     HttpContext.Current.Response.AddHeader("Location", StringUrl);}

Now if you do a Response. you will see the static helper method in the intellisense.

ResponsePermanentRedirect