This post explains about Dynamically assigning Metadata to a page using code behind in ASP.NET 4.0. Metadata is so important as Search engine optimization considers Metadata keywords and description to index the page. If you can dynamically assign the metadata to your page then search engine can easily analyze and puts the page in results list.
1. Create a ASP.NET web application in VS 2010 and add a grid view to the page
2. Configure the SqlDataSource to grid view with Address table as shown below
3. Run the application then you will see the following page
4. Right click the above and you will notice that there wonâ€™t be any meta tags in the page source.
This could be problematic for SEO search engine. One solution is you can go to the page and statically can give the meta data description and keywords. This wonâ€™t help for the website where data is dynamically updated and want to update their page meta tags according to their generated data.
5. Now We will display the list of sales persons from particular city and update the meta tags accordingly.
6. In code behind for the above page write the following code
string strCity = Page.RouteData.Values["State"] as string; Page.MetaDescription = "A List of sales persons from " + strCity; Page.MetaKeywords = "Lists, Sales person" + strCity;
The above code gets the city value from the routing URL and then appends the value dynamically to the page meta description and keywords.
Now run the application and you will see the following output in the page view source.
You can notice that meta tags are update based on the visited cities and this description updated dynamically whenever the city value changed in the URL.
|Share this post :|