Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

Web Forms Routing in ASP.NET 4

At our first Sarasota Web Developer Group meeting we discussed several of the new enhancements in ASP.NET 4 Web Forms. One of my favorite enhancements is the new routing features which are very similar to the ones I have enjoyed so much in ASP.NET MVC.

 

Register Routes

This is old hat for those using ASP.NET MVC. Just register your routes at application startup. Rather than your endpoint being a controller, however, you associate a physical page as the handler of the request.

 

public class Global : System.Web.HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes(RouteTable.Routes);
    }

    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute(
            "Contact_Details",                  // Route Name
            "Contacts/Details/{id}",          // Url and Parameters
            "~/Contacts/Details.aspx"     // Page Handling Request
            );
    }
}

 

In this case we are telling the routing engine 3 things:

  • Name of the Route: Contact_Details
  • The Route: Contacts/Details/{id}
  • The Physical Page Handling the Request: Details.aspx

 

Notice the id parameter ( route value ) which will be the id of the contact to display in the details page.

 

Expression Builders for Creating HyperLinks, etc.

With ASP.NET MVC we have strongly-typed View Helpers to help generate links. With ASP.NET 4 Web Forms you utilize Expression Builders to create links such as:

 

<asp:HyperLink
    NavigateUrl="<%$RouteUrl:RouteName=Contact_Details, id=1 %>"
    runat="server">John Doe
</asp:HyperLink>

<asp:HyperLink
    NavigateUrl="<%$RouteUrl:id=1%>"
    runat="server">John Doe
</asp:HyperLink>

 

We can explicitly specify the name of the route or let the routing engine figure out the correct route based on the parameters.

 

Getting RouteData from the Page

You can access the RouteData from the Page by accessing the Page.RouteData Property, which is just a convenient access point to RequestContext.RouteData. Here are a couple of ways to get the id from the route to display the proper contact given its id:

 

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        var id = Page.RouteData.GetRequiredString("id");

        var id2 = Page.RouteData.Values["id"];
    }
}

 

RouteParameter for use with DataSources

If you are displaying the contact in a DetailsView, for example, you can use the new RouteParameter with your DataSource to get values from the route as such:

 

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" 
    SelectMethod="FindById" TypeName="Contact">
    <SelectParameters>
        <asp:RouteParameter Name="id" RouteKey="id" Type="Int32" />
    </SelectParameters>
</asp:ObjectDataSource>

 

Binding your DetailsView to the ObjectDataSource will now cause the contact to be displayed appropriately.

 

Response.RedirectToRoute and RedirectToRoutePermanent

A lot has been mentioned about using Response.RedirectPermanent for SEO, but even cooler is Response.RedirectToRoute and Response.RedirectToRoutePermanent for working with the new routing engine. Below I am specifying the route name and passing in any route values where necessary when redirecting:

 

Response.RedirectToRoute("Contact_Details", new { id = 1 });

Response.RedirectToRoutePermanent("Contact_Details", new { id = 1 });

 

Conclusion

Lots of really neat things in ASP.NET 4 and ASP.NET 4 Web Forms. I am going to continue to post a few more we discussed during the first meeting.

For those interested, the second meeting of the Sarasota Web Developer Group will discuss a number of interesting topics: Leveraging ASP.NET MVC – Web Forms – DynamicData – Castle ActiveRecord.

 

David Hayden

 

Posted in Uncategorized | 1 Comment

Sarasota Web Developer Group – MVC and ASP.NET 4 From Scratch

Teaching ASP.NET 4 and ASP.NET MVCLast year I organized the Tampa ASP.NET MVC Developer Group that successfully taught ASP.NET MVC from scratch to around 75 regular attendees on a monthly basis.

Given its success I decided to do the same thing in Sarasota, Florida, except that instead of focusing just on ASP.NET MVC, we will teach the new enhancements in ASP.NET 4 as well as ASP.NET MVC from scratch. Just like the Tampa MVC Developer Group, meetings will build upon one another, increasing the skills and confidence of its members each month. In addition to monthly presentations on both ASP.NET 4 and ASP.NET MVC, we will also have

  • Monthly newsletters filled with resources and links.
  • Monthly screencasts that provide a quick summary of material presented.
  • Optional monthly homework assignments that challenge members.
  • A Windows Live Group to ask questions and share knowledge.
  • Wonderful sponsors that make all of this fun and possible!

Our first meeting is this Wednesday, February 24, 2010. I am still working on a website, but for those interested you can read the following post which explains a little bit about the group, upcoming meeting dates and registration for the first meeting:

 

 

The group is perfect for those interested in learning about Microsoft web development for the first time, ASP.NET WebForms developers excited about leveraging the new features in ASP.NET 4, and developers interested in learning the ASP.NET MVC Framework.

If you are interested in attending, make sure you register for the first meeting mentioned in the link above and feel free to download the first monthly newsletter.

In the upcoming weeks I will start to post on CodeBetter about the various topics and code samples we discuss at the meetings. The posts will be more introductory at first and then begin to get more advanced as we discuss more in-depth topics.

If you live in the Sarasota, Florida area and have any interest in Microsoft web development, join in on the fun!

Posted in Uncategorized | 1 Comment

Integrating O/R Mappers into ASP.NET MVC at Tampa MVC Developer Group

We have another exciting meeting at the Tampa ASP.NET MVC Developer Group this Thursday, September 17, 2009 at 6:30pm at the Microsoft Office in Tampa.

The focus is on getting started and proven practices at integrating an O/R Mapper into your ASP.NET MVC Web Applications. Two speakers will be presenting for 1 hour each on using NHibernate and SubSonic in your ASP.NET MVC Web Applications.

Not only will you get a solid guide to using SubSonic and NHibernate, but the discussions will transcend specific O/R Mappers to help you understand how to integrate any O/R Mapper ( Entity Framework, LINQ To SQL, etc. ) using proven practices into your applications.

Although the context of the presentations will be from an ASP.NET MVC perspective, those developing in Winforms, WPF, Webforms, etc. will surely get a lot out of the presentations. Therefore, even if you don’t use ASP.NET MVC, please come out to the meeting and join in on all the free training, lively discussions, pizza, drinks, and great giveaways!!

 

Please register here.

 

Look forward to seeing you there!

 

David Hayden

Posted in Uncategorized | 2 Comments

ASP.NET MVC 2.0 Preview 1 Presentation Sample Code

I gave a quick 20 minute presentation on ASP.NET MVC 2.0 Preview 1 at the Tampa ASP.NET MVC Developer Group Meeting on Wednesday which was based on the following ASP.NET MVC 2.0 Tutorials.

I have uploaded the sample code for those who attended:

 

I hope it is useful!

Posted in Uncategorized | Leave a comment

Tampa ASP.NET MVC August Meeting – 4 Great Sessions!

Really excited about the next Tampa ASP.NET MVC Developer Group Meeting scheduled for Wednesday, August 26, 2009.

We have 4 local developers presenting great sessions on the ASP.NET MVC Framework. Topics include:

  • Adding a forum to your existing ASP.NET MVC Website
  • Adding and Using the Spark View Engine
  • Integrating OpenId in your MVC Application
  • Exploring ASP.NET MVC 2.0 Preview 1

 

You can register here.

 

See you there!

 

Recent Posts:

Posted in Uncategorized | Leave a comment