Table of Contents


AspxPage Class Reference

Overview

The AspxPage class is a static helper class that provides utility methods for working with Magentrix MVC controllers. It encapsulates common operations such as parameter retrieval, message handling, error management, reference information extraction, and cookie manipulation.


Methods Summary

MethodSignature
GetParameterpublic string GetParameter(string name)
AddMessagepublic void AddMessage(string summary)
AddWarningpublic void AddWarning(string summary)
AddMessagepublic void AddMessage(string title, string summary, MessageType type)
AddErrorpublic void AddError(string summary, string propertyName = null)
AddErrorpublic void AddError(Exception ex)
SetCookiepublic void SetCookie(System.Web.HttpCookie cookie)
GetCookiepublic System.Web.HttpCookie GetCookie(string name)
RemoveCookie     public void RemoveCookie(string name)

Methods

GetParameter

Retrieves a parameter value from either the request query string/form data or route data.

Signature

public string GetParameter(string name)

Parameters

NameTypeDescription
name  string  The name of the parameter to retrieve  

Returns

  • Type:string
  • Description: The parameter value if found; otherwise, null

Example

var userId = AspxPage.GetParameter("userId");

if (!string.IsNullOrEmpty(userId))
{
    // Process the user ID
}

AddMessage (Summary Only)

Adds an informational message to the view messages collection.

Signature

public void AddMessage(string summary)

Parameters

NameTypeDescription
summary  string  The message text to display  

Example

AspxPage.AddMessage("Operation completed successfully.");

AddWarning

Adds a warning message to the view messages collection.

Signature

public void AddWarning(string summary)

Parameters

NameTypeDescription
summary  string  The warning text to display  

Example

AspxPage.AddWarning("This action cannot be undone.");

AddMessage (Full)

Adds a custom message with a specified title and message type to the view messages collection.

Signature

public void AddMessage(string title, string summary, MessageType type)

Parameters

NameTypeDescription
titlestringThe message title/header
summarystringThe message text to display
type  MessageType  The type of message (e.g., Message, Warning, Error)  

Example

AspxPage.AddMessage("Success", "Record saved successfully.", MessageType.Message);
AspxPage.AddMessage("Alert", "Please review the changes.", MessageType.Warning);

AddError (With Property Name)

Adds a validation error to the model state, optionally associated with a specific property.

Signature

public void AddError(string summary, string propertyName = null)

Parameters

NameTypeDescription
summarystringThe error message text
propertyName  string  (Optional) The name of the property associated with the error. If null, adds a model-level error  

Example

// Property-specific error
AspxPage.AddError("Email address is required", "Email");

// Model-level error
AspxPage.AddError("An unexpected error occurred");

AddError (From Exception)

Adds error information to the model state from an exception object.

Signature

public void AddError(Exception ex)

Parameters

NameTypeDescription
ex  Exception  The exception object containing error details  

Example

try
{
    // Perform operation
}
catch (Exception ex)
{
    AspxPage.AddError(ex);
}
💡 Note: This method provides special handling for ValidationException objects, which may contain multiple validation errors.

SetCookie

Adds a cookie to the HTTP response.

Signature

public void SetCookie(System.Web.HttpCookie cookie)

Parameters

NameTypeDescription
cookie  HttpCookie  The cookie object to add to the response  

Exceptions

Exception TypeCondition
ValidationExceptionThrown if attempting to set a cookie with the name matching FormsAuthentication.FormsCookieName 

Example

var cookie = new HttpCookie("UserPreference")
{
    Value = "darkMode",
    Expires = DateTime.Now.AddDays(30)
};
AspxPage.SetCookie(cookie);
⚠️ Warning: Cannot be used to set the Forms Authentication cookie. Use the appropriate authentication methods instead.

GetCookie

Retrieves a cookie from the HTTP request.

Signature

public System.Web.HttpCookie GetCookie(string name)

Parameters

NameTypeDescription
name  string  The name of the cookie to retrieve  

Returns

  • Type:HttpCookie
  • Description: The requested cookie if found; otherwise, null

Exceptions

Exception TypeCondition
ValidationExceptionThrown if attempting to retrieve a cookie with the name matching FormsAuthentication.FormsCookieName 

Example

var userPrefCookie = AspxPage.GetCookie("UserPreference");
var userPreference = userPrefCookie?.Value;
⚠️ Warning: Cannot be used to retrieve the Forms Authentication cookie for security reasons.

RemoveCookie

Removes a cookie from the client by setting its expiration date to the past.

Signature

public void RemoveCookie(string name)

Parameters

NameTypeDescription
name  string  The name of the cookie to remove  

Exceptions

Exception TypeCondition
ValidationExceptionThrown if attempting to remove a cookie with the name matching FormsAuthentication.FormsCookieName

Behavior

Calls the AspxPage.RemoveCookie() helper method to expire the cookie.

Example

AspxPage.RemoveCookie("UserPreference");
⚠️ Warning: Cannot be used to remove the Forms Authentication cookie.

Usage Notes

Message Handling

The class provides three methods for adding messages to the view:

  • AddMessage(string summary) — For informational messages
  • AddWarning(string summary) — For warning messages
  • AddMessage(string title, string summary, MessageType type) — For custom messages

All messages are stored in the controller's ViewMessages collection and can be rendered in the view.

Error Handling

The class provides flexible error handling:

  • Property-specific errors using AddError(string summary, string propertyName)
  • General model errors using AddError(string summary)
  • Exception-based errors using AddError(Exception ex)

Errors are added to the ASP.NET MVC ModelState for validation feedback.

Cookie Security

All cookie methods (SetCookie, GetCookie, RemoveCookie) include built-in protection against manipulating the Forms Authentication cookie, ensuring that authentication security is maintained.


Dependencies

  • AspxController — The controller instance that provides access to request, response, routing, and model state

See Also