If you are using ASP.NET WebForms you would need to create a HttpHandler
or use RouteCollection
. In the case of ASP.NET MVC just add a route.
ASP.NET WebForms - Form 1 (any version)
Creating the route:
public class EmployeeHandlerFactory : IHttpHandlerFactory
{
...
public IHttpHandler GetHandler(HttpContext context,
string requestType, string url, string pathTranslated)
{
// determine the employee's name
string empName =
Path.GetFileNameWithoutExtension(
context.Request.PhysicalPath);
// Add the Employee object to the Items property
context.Items.Add("Employee Info",
EmployeeFactory.GetEmployeeByName(empName));
// Get the DisplayEmployee.aspx HTTP handler
return PageParser.GetCompiledPageInstance(url,
context.Server.MapPath("DisplayEmployee.aspx"), context);
}
}
Creating the page
public class DisplayEmployee : System.Web.UI.Page
{
// three Label Web controls in HTML portion of page
protected System.Web.UI.WebControls.Label lblName;
protected System.Web.UI.WebControls.Label lblSSN;
protected System.Web.UI.WebControls.Label lblBio;
private void Page_Load(object sender, System.EventArgs e)
{
// load Employee information from context
Employee emp = (Employee) Context.Items["Employee Info"];
if (emp != null)
{
// Assign the Employee properties to the Label controls
lblName.Text = emp.Name;
lblSSN.Text = emp.SSN;
lblBio.Text = emp.Biography;
}
}
Updating web.config
:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<httpHandlers>
<!-- EmployeeHandlerFactory -->
<add verb="*" path="*.info"
type="skmHttpHandlers.EmployeeHandlerFactory,
skmHttpHandlers" />
</httpHandlers>
</system.web>
</configuration>}
Final effect:
ASP.NETWebForms-Form2(ASP.NET3.5+Only)
IntheGlobal.asax
fileinclude:
protectedvoidApplication_Start(objectsender,EventArgse){RegisterRoutes(RouteTable.Routes);}publicstaticvoidRegisterRoutes(RouteCollectionroutes){routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx");
}
ASP.NET MVC
Add the route directly to your application class:
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}