Lesson 1
Customizing the User Experience within ASP
The earliest Web sites were collections of independent, mostly static HTML pages joined together only by links.
These sites offered little in the way of interactivity and did not profile users to customize their experience as is now commonplace.
ASP has many features that you can use to add customization to a user's visit and to your Web site, and
we will cover them in this module. By the end of this module, you will be able to:
- Explain why the lack of "state" on the Web causes difficulty in retaining user data
- Explain how the Session object and session variables maintain user information throughout a site visit
- Describe and create a Global.asa file with common user start and end procedures
- Display a banner image or text that rotates through a series with the Advertisement Rotator component
- Explain how the Server object can URL encode data that includes a space or other character not allowed inside a URL
The next lesson describes problems caused by lack of "state" on the Web and some solutions.
ASP.NET Core in Action
ASP.NET as a Service Framework
In the years since the first release of the .NET Framework, Microsoft has provided a variety of approaches for building service-oriented applications. Starting back in 2002 with the original release of .NET, a developer could fairly easily create an ASP.NET ASMX-based XML web service that allowed other .NET and non-.NET clients to call it.
Those web services implemented various versions of SOAP, but they were available for use only over HTTP.
In addition to support for web services, the 1.0 release of .NET provided support for Remoting. This allowed developers to write services that were not necessarily tied to the HTTP protocol. Similar to ASMX-based web services, .NET Remoting essentially provides object activation and session context for client-initiated method calls.
The caller uses a proxy object to invoke methods, and the .NET runtime handles the serialization and marshaling of data between the client's proxy object and the server's activated service object.
Towards the end of 2006, Microsoft released .NET 3.0, which included the Windows Communication Foundation (WCF).
WCF not only replaced ASMX web services and .NET Remoting, but also took a giant step forward in the way of flexibility, configurability, extensibility,
and support for more recent security and other SOAP standards. For example, with WCF, a developer can write a non-HTTP service that supports authentication with SAML tokens and host it in a custom-built Windows service.
These and other capabilities greatly broaden the scenarios under which .NET can be utilized to build a service-oriented application.
Add Customization to a User's Visit and website using ASP.NET
To add customization to a user's visit on a website using "ASP.NET as a Service Framework", you can leverage features like 1) Sessions,
2) Cookies, 3) Authentication, and 4) Personalization in ASP.NET.
Below is an outline of the various approaches you can use:
- Sessions:
Sessions in ASP.NET store user-specific information on the server side, which can be retrieved on subsequent requests to customize the user experience.
- How to use sessions:
- Set session data:
HttpContext.Session.SetString("UserName", "JohnDoe");
- Get session data:
var userName = HttpContext.Session.GetString("UserName");
- Customization example: You can track a user's preferences (e.g., theme, language) across their visit by saving their choices in a session and modifying the UI accordingly.
- Cookies:
Cookies store small pieces of information on the user's browser, and they persist between visits, allowing you to track returning users.
- How to use cookies:
- Set cookie:
var cookieOptions = new CookieOptions
{
Expires = DateTime.Now.AddDays(7)
};
Response.Cookies.Append("Theme", "DarkMode", cookieOptions);
- Get cookie:
var theme = Request.Cookies["Theme"];
- Customization example:
Use cookies to remember a user's selected theme or preferences so the website will load with their choices when they return.
- Authentication and Authorization:
Use ASP.NET Identity to authenticate users and customize their experience based on their roles or claims.
- Customization example: You can display different content or layouts based on the userâs role (admin, user, guest), or show personalized dashboards with user-specific data.
- Basic setup:
- Use `SignInManager` to sign users in and track them.
var result = await _signInManager.PasswordSignInAsync(userName, password, isPersistent, lockoutOnFailure);
- Display different content based on roles:
if (User.IsInRole("Admin"))
{
// Show admin-specific content
}
else
{
// Show general user content
}
- Personalization via Profiles:
ASP.NET supports profile properties, which allow you to persist user-specific data across sessions.
-
Setting up profiles in Web.config:
<profile>
<properties>
<add name="PreferredLanguage" type="String" />
<add name="Theme" type="String" />
</properties>
</profile>
-
Accessing profile data in code:
Profile["PreferredLanguage"] = "English";
var language = (string)Profile["PreferredLanguage"];
- Customization example: You can store user preferences such as language and display customized content based on their stored profile information.
- Using APIs for Dynamic Data Customization:
If your ASP.NET application is consuming backend services or APIs, you can fetch dynamic, user-specific data from the API and adjust the UI based on the response.
Customization example: After a user logs in, you can retrieve their profile or transaction data from an API and use it to display a personalized dashboard.
- MVC View Components / Razor Pages for Dynamic Content:
In ASP.NET Core, you can use View Components or Razor Pages to customize sections of the UI dynamically based on the userâs data.
- Using Razor Pages:
You can return different content from `Razor Pages` based on the userâs context, such as their preferences or role.
Example: Custom Welcome Message with Session and ViewBag
You can greet users by name based on their stored session or profile data.
Controller Action:
public IActionResult Index()
{
string userName = HttpContext.Session.GetString("UserName") ?? "Guest";
ViewBag.UserName = userName;
return View();
}
View (Razor):
<h2>Welcome, @ViewBag.UserName!</h2>
Conclusion:
By utilizing ASP.NET's built-in features like 1) Sessions, 2) Cookies, 3) Authentication, and 4) Profiles, you can create a personalized and customized experience for users during their visits.