Se você estiver usando ASP.NET WebForms teria de criar um HttpHandler
ou usar o RouteCollection
. No caso do ASP.NET MVC basta adicionar uma rota.
ASP.NET WebForms - Forma 1 (qualquer versão)
Criando a rota:
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);
}
}
Criando a página
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;
}
}
Atualizando o 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>}
Efeito final:
ASP.NET WebForms - Forma 2 (Apenas ASP.NET 3.5+)
No arquivo Global.asax
inclua:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("",
"Category/{action}/{categoryName}",
"~/categoriespage.aspx");
}
ASP.NET MVC
Adicione a rota diretamente na classe de sua aplicação:
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);
}
}