Enter page using the bank ID in ASP.NET

4

I need to make a page that can be accessed in the following format:

www.exemplo.com/id/123qwe

This 123qwe is the ID of a line in the database, where I will get all the data to fill the page.

So far I've been able to do www.exemplo.com/id/id.aspx?id=123qwe using:

<%=Request.QueryString["id"] %>

How do I use www.exemplo.com/id/123qwe instead of www.exemplo.com/id/id.aspx?id=123qwe ?

    
asked by anonymous 24.02.2014 / 14:15

3 answers

3

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.asaxfileinclude:

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);
    }
}
    
24.02.2014 / 15:01
3

You can use ASP.NET MVC , which implements this automatically for you. The default route for an MVC project (already created at first) is as follows:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { action = "Index", id = UrlParameter.Optional }
);

Which means you would have a URL of type Home / Index / 123qwe running by default. It would be enough to read this ID in your controler and use it:

public ActionResult Index(string id)
    
24.02.2014 / 14:38
1

.Net 4.0 onwards it is already possible to use the MVC routes in webforms project, I myself use it. At Global.asax you should log the routes.

routes.MapPageRoute("SalesRoute",
      "SalesReport/{locale}/{year}",
      "~/sales.aspx");

And to access the value in the aspx page you use the following code:

Page.RouteData.Values["locale"]
Page.RouteData.Values["year"]
    
25.02.2014 / 18:42