Prop Webcontrol Adrotator Advertisementfile
## ASP.NET AdRotator AdvertisementFile Property
The `AdvertisementFile` property is a core attribute of the ASP.NET `AdRotator` Web Server Control. This tutorial provides a comprehensive guide on how to configure and use this property to display rotating advertisements on your web pages.
---
## Definition and Usage
The `AdvertisementFile` property gets or sets the path to an XML file that contains the advertisement data.
The `AdRotator` control reads this XML file to retrieve a list of ads, their associated images, navigation URLs, impressions (weight), and alternate text. Each time the page is loaded or refreshed, the control randomly selects and displays one of the ads defined in the XML file based on its configured weight.
---
## Syntax
```asp
```
### Property Values
| Property Value | Description |
| :--- | :--- |
| `path` | A string value specifying the location of the XML file containing the advertisement data. |
The `path` can be specified in three different formats:
* **Absolute Path:** Points to a fully qualified URL (e.g., `http://www.yourdomain.com/ads/billboard.xml`).
* **Application-Relative Path (Virtual Root):** Uses the tilde (`~`) operator to resolve paths relative to the application root directory (e.g., `~/App_Data/ads.xml`). This is the recommended approach for portability.
* **Relative Path:** Points to a location relative to the current page's directory (e.g., `ads/sidebar_ads.xml` or `../ads.xml`).
---
## Code Examples
To implement the `AdRotator` control, you need two components: the **XML Advertisement File** and the **ASP.NET Web Form (.aspx)**.
### Step 1: Create the XML Advertisement File (`Ad1.xml`)
The XML file must follow a specific schema. The root element must be ``, and each individual ad is defined within an `` element.
```xml
~/images/banner1.jpg
https://www.youtip.co/learn-dotnet
Learn .NET Core on YouTip
40
Education
~/images/banner2.jpg
https://www.youtip.co/cloud-hosting
Reliable Cloud Hosting Services
60
Hosting
```
#### Standard XML Elements Explained:
* ``: The path to the image file to display for the ad.
* ``: The URL the user will navigate to when they click the ad.
* ``: The alternate text displayed if the image is unavailable, or used as tooltips in some browsers.
* ``: A relative weight value indicating how often the ad should be displayed compared to other ads in the file. (e.g., an ad with impressions set to 60 will appear 1.5 times more often than an ad with impressions set to 40).
* ``: A category keyword used to filter ads using the `KeywordFilter` property of the `AdRotator` control.
### Step 2: Declare the AdRotator Control in your ASPX Page
Reference the XML file in your `.aspx` markup using the `AdvertisementFile` property:
```html
<%@ Page Language="C#" %>
AdRotator AdvertisementFile Example
```
---
## Considerations and Best Practices
1. **Security and File Location:**
It is highly recommended to store your advertisement XML files inside the `App_Data` folder (e.g., `~/App_Data/ads.xml`). IIS is configured by default to block direct HTTP requests to files inside the `App_Data` folder, preventing external users from downloading your ad configuration file.
2. **Caching Issues:**
If your ASP.NET page uses output caching (`<%@ OutputCache %>`), the `AdRotator` control will not rotate ads on every page refresh because the entire page is cached. To bypass this, you can place the `AdRotator` inside a `Substitution` control or use post-cache substitution techniques.
3. **Dynamic File Assignment:**
You can programmatically change the `AdvertisementFile` property in your code-behind file based on user roles, localization, or page context:
```csharp
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
AdRotator1.AdvertisementFile = "~/App_Data/premium_ads.xml";
}
}
```
YouTip