WebPages Helpers
ASP.NET WebPages Helpers provide reusable components that simplify common tasks, such as rendering images, sending emails, and creating forms. Helpers make it easy to include advanced functionality in your web applications.
Key Topics
Using Helpers
Example
@{
var imageUrl = "https://example.com/image.jpg";
}
<h2>Using Image Helper</h2>
<p>@Html.Raw("
")</p>
Explanation: This example uses Razor syntax to render an image by dynamically generating an HTML img
tag with a source URL.
Displaying Images Dynamically
Example
@{
var imagePath = Url.Content("~/Content/Images/sample.jpg");
}
<p><img src="@imagePath" alt="Dynamic Image" /></p>
Explanation: This example dynamically generates the image source path using the Url.Content
helper to ensure proper resolution for server-relative paths.
Sending Email
Example
@{
WebMail.SmtpServer = "smtp.example.com";
WebMail.SmtpPort = 587;
WebMail.EnableSsl = true;
WebMail.UserName = "your-email@example.com";
WebMail.Password = "your-password";
WebMail.Send(
to: "recipient@example.com",
subject: "Test Email",
body: "This is a sample email from ASP.NET WebPages.",
isBodyHtml: false
);
}
<p>Email sent successfully.</p>
Explanation: The WebMail
helper is configured with SMTP server details and used to send an email with a specified recipient, subject, and body.
Key Takeaways
- Helpers like
WebMail
andUrl.Content
simplify common tasks in web development. - The
Html.Raw
method allows embedding dynamic content safely into your pages. - Helpers are reusable components designed to save time and reduce boilerplate code.