URL Routing With ASP.NET 4 - Web Forms

by Prashant 27. June 2010 03:38

URL Routing was first introduced in .NET framework 3.5 SP1 but MVC has built-in and works pretty decently to create SEO friendly URL and prevents URL hacking. ASP.NET 4.0 is now introduced with a new feature called URL routing with web forms. URL routing help developers to create short and friendly URLs which enhance search engine page ranking. There are few other ways to create short friendly URLs like URLrewriter.net extension or if you have a physical access to IIS you can have installed URL Rewriter extension for IIS 7 to create short friendly URLs. Hey! not everyone has access to IIS!! So if you don't have the access then also you can re-write the URLs using this new feature in ASP.NET 4.0. 

One thing I would like to mention is that when you create a new ASP.NET web application in Visual Studio 2010, it won't show up with a blank page, but instead build a full applicaton with sample pages with a pretty good design. As you see below I haven't design this page..actually this is a default template when you create a new ASP.NET web application. What I have done here is just put a text box to enter contact ID and a button to get the details from the Adventure Works sample database.

 

This is a pretty simple interface and now we take a look at some internal work of this web application. My primary focus is on having simple URLs for my application for better search engine optimizations. This application has two main pages apart from the about and other pages that added to the project through the template. The Default.aspx is the main page where we have a field which allow the user to enter the contact ID for the person he want to search. The other page which handles the request and show the details of the contact person is called View.aspx. But this is a really tricky part from a user's perspective as a user will never see this page on the browser address bar while navigating a website. Check the View.aspx design code and check the <asp:SqlDatasource> tags and notice the SelectParameter tag. As I am using a select query with a parameter to populate the grid, the SelectParameter tag further uses <asp:RouteParameter> with route name and route key.

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" 
        SelectCommand="select Title,FirstName,MiddleName,LastName, EmailAddress from Person.Contact where ContactID=@id">
        <SelectParameters>
            <asp:RouteParameter Name="id" RouteKey="id" />
        </SelectParameters>
    </asp:SqlDataSource>

OK! Let's start up what we have on the Default.aspx page. This is the default page and a user will see this page first. Write the below code on the button to redirect the request to route handler. Here I have used a Regex expression to validate if the user enters a numeric ID and not any alphanumeric or alphabet. This check is just a workaround, I recommend you to use a better validation technique.

if((Regex.IsMatch(txt_pid.Text.Trim(), @"\d+")) == true)
{
          Response.RedirectToRoute("Persons-Details",
          new { id = txt_pid.Text });
}

So does this URL make any sense? Not at this moment but surely it will after if you have registered your routes in the Global.asax file. My Global.asax file has a method called void RegisterRoutes(RouteCollection Route)

void RegisterRoutes(RouteCollection routes)
{
       RouteTable.Routes.MapPageRoute("Persons-Details",
         "Person/{id}", 
         "~/View.aspx");
}

The method MapPageRoute accepts some parameters. The first parameter - "Person-Details" you see is the name of the Route which can be any thing you like. The second parameter - "Person/{id}" is the URL which we have generated. In short this is the URL which is visible to the user and outside world, what is happening internally only a developer knows!. The third parameter - "~/View.aspx" is the physical file which actually process the request and return it to the second parameter. The second parameter is the route URL and you can name it what you like except the parameter you are passing, just make sure you use the same parameter name everywhere. In the method void RegisterRoutes(RouteCollection Route) you can register number of route handlers in a single go under void Application_Start method in the Global.asax file.

void Application_Start(object sender, EventArgs e){
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}

Once you registered the routes in the Global.asax file, you are done. Time to press F5 and see the action. On the default page enter the ID and hit the Get Details button. The page rendered in front of you / user will be having a clean tidy URL.

 

 Download: ASP.NET4URLRouting.zip (175.58 kb)

Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , , ,

.NET Framework | ASP.NET | C#

Build “Out of Browser” Applications with Silverlight

by Prashant 20. June 2010 18:30

If you think that Silverlight applications are only intended to be run on web browsers, then you are wrong! Silverlight is now widely used to build Rich Internet Application (RIA) allowing users to work more efficiently and make a working session more interactive and attractive. As we all know a customer will always seek for a short-cut method, he will never listen to your comments or suggestions and wants everything to be done in few mouse clicks. A lazy customer! You build an application with a lot of effort and upload it to the user or customer’s website. Now the customer wants the same application for his desktop - he doesn’t want to open browser and work. Writing the same application again for his desktop won’t help and will also waste a lot of development time, why can’t we make the same Silverlight application to run from his machine?

Silverlight gives you an option to install the Silverlight application from the browser to your desktop called “Out of Browser” application support. Consider if you have built a game on Silverlight and upload it to your blog/website allowing you reader or visitors to play the game. It’s not just the case with games but same can be done with applications or with some general web utility like URL shortening service using Bit.ly API.

To get started create a new Silverlight application in Visual Studio 2010.

Give the name to your application and click OK.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 Design your application put the business logic in (if any). Once you are done designing and building the application go to the project properties. Click Project > APPNAME Properties. APPNAME represents the name of your application. In the project properties check the “Enable running application out of browser”.

Out-of-Browser Settings button will get enabled which will let you choose out of browser application settings.

The settings dialogue box is quite self-explanatory. You can set the name of the application which you want to put on the window when it runs out of the browser. You can also set the icon for you application in different sizes. Click OK to save settings.

Now, run you application. You will see your Silverlight application running inside your browser. If you want the application to run out of the browser then right-click anywhere in the browser and click “Install <APPNAME> onto this computer”.

This will prompt you to install the application and place the application icons on your desktop or on your start menu or both. It will give your application the default application install icon. If you have set your own in the Out of Browser Settings then you will see your icon.

 

Now your Silverlight application is on your system and can be executed at user’s desktop. Check out the same demo here. It's just a demo anyway!

Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: ,

Silverlight | Visual Studio

Windows Phone 7 - Supported Media Codecs For Audio and Video

by Prashant 11. June 2010 16:52

Today I was just looking at some of the demos on Windows Phone development and I came across a list of all the supported media codecs for Windows Phone 7. here is the list of all the supported audio and video codecs supported by

Codec Type

Decoder Support

Container

Audio

WAV (PCM, MSADPCM, IMAADPCM, G.711)

WAV

Audio

MP3

MP3

Audio

WMA Lossless

ASF (WMA)

Audio

WMA Pro

ASF (WMA)

Audio

WMA Standard v9

ASF (WMA)

Audio

AAC-LC (Low Complexity)

3GP, 3G2, MP4, M4A

Audio

HE-AAC v1 (AAC+)

3GP, 3G2, MP4, M4A

Audio

HE-AAC v2 (eAAC+)

3GP, 3G2, MP4, M4A

Audio

Adaptive Multi-Rate Narrow Band (AMR-NB)

3GP, 3G2, MP4

Audio

Adaptive Multi-Rate ide Band (AMR-WB)

3GP, 3G2, MP4

Audio

Qcelp

3GP, 3G2, MP4

Video

WMV (VC-1) - Simple Profile

ASF (WMV)

Video

WMV (VC-1) - Main Profile

ASF (WMV)

Video

WMV (VC-1) - Advanced Profile

ASF (WMV)

Video

WMV v9

ASF (WMV)

Video

MPEG-4 Part 2 - Simple Profile

3GP, 3G2, MP4, AVI

Video

MPEG-4 Part 2 – Advanced Simple Profile

AVI, MP4

Video

DivX 4.x/5.x/6.x

AVI

Video

MPEG-4 Part 10 (MPEG-4 AVC, H.264) - Baseline Profile

3GP, 3G2, MP4, M4V

Video

MPEG-4 Part 10 (MPEG-4 AVC, H.264) - Main Profile

3GP, 3G2, MP4, M4V

Video

MPEG-4 Part 10 (MPEG-4 AVC, H.264) - High Profile

3GP, 3G2, MP4, M4V

Video

H.263

3GP, 3G2

Images

JPEG

JPG

Images

PNG

PNG

Images

GIF (both GIF87a and GIF89a)

GIF

Original Source: Windows Phone 7 Media Codec Support

Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: , ,

Microsoft

First Look At Microsoft Office 2010 - Free E-book From Microsoft Press

by Prashant 11. June 2010 02:27

I just installed Microsoft Office Professional Plus 2010 and now I am experiencing some new exciting features of Microsoft Office. Here is free e-book from Microsoft press for users to have a glance at the latest version of their Office series. If you haven't tried it but want to go for Office 2010, then take a look at this free e-book.

Download E-Book: PDF Version | XPS Version

Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags: ,

Microsoft

URL rewriting in Blog Engine.NET

by Prashant 5. June 2010 23:47

I was just looking for a way to get short URL’s for my blog, I used some extensions, some URLrewriter.NET but still not able to get this work. So I drop an e-mail to Scott Hanselman and was waiting for his response. If you look at Scott’s blog you will see that he has very simple URL for his blog posts which eventually get his posts higher ranks on search engine indexing. What I have found so far is that if you have a simple URL the search engine will crawl that page during indexing and your page is on the top of the search results or if it is not on the top then it must be at least on the first page. Remember search engine crawlers will avoid crawling pages that do not have a simple URL, like if your page has a query string it won’t be get crawled by the search engine crawler.

The URLRewriter module in BlogEngine didn’t seems to be working for Posts. I want to have simple SEO friendly URLs like http://www.midnightprogrammer.net/post/Windows-7-Logon-Screen-Changer.aspx and not http://www.midnightprogrammer.net/post/2010/05/30/Windows-7-Logon-Screen-Changer.aspx But there is no way to do this until I hook into BlogEngine.Core.dll.

Open the whole solution in Visual Studio and open Post.cs file and navigate to the RelativeLink property in Post.cs file and change with the below code:

public string RelativeLink
{
	get
	{
		string slug = Utils.RemoveIllegalCharacters(Slug) + BlogSettings.Instance.FileExtension;
		if (BlogSettings.Instance.TimeStampPostLinks)
			return Utils.RelativeWebRoot + "post/" + slug;
		return Utils.RelativeWebRoot + "post/" + slug;
	}
}

Recompile your DLL and place in bin directory in your blogengine root folder. Now you will have short URLs which do not have any date stamp. Having short URLs for all your posts doesn’t affect the posts with old URLs that most of the people have bookmarked on their blogs or on social bookmarking sites. Hope this helps.

Share or Bookmark this post…
  • Live
  • Facebook
  • TwitThis
  • del.icio.us
  • Digg
  • DZone
  • Technorati
  • StumbleUpon
  • Google
  • E-Mail

If you enjoyed this post, make sure you subscribe to my RSS feed!

Tags:

BlogEngine.NET

Powered by BlogEngine.NET 1.5.0.7
Visit blogadda.com to discover Indian blogs

About

Name of authorPrashant Khandelwal

Programmer and Tech Enthusiast



       

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2010

Creative Commons License