Log into page using the bank ID in ASP.NET

Asked

Viewed 629 times

4

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

www.exemplo.com/id/123qwe

That one 123qwe is the ID of a database line, where I’ll get all the data to fill out the page.

So far I’ve done 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?

  • you are using webForms or MVC?

3 answers

3

You can use ASP.NET MVC, that automatically implements this for you. The default route of a MVC project (already created from the beginning) 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 the type Home/Index/123qwe working by default. Then just read this ID in your controller and use it:

public ActionResult Index(string id)
  • I’m using an ASP.Net Webforms project with Devexpress. ?

  • There is no way @Gutoschiavon that is ASP.NET MVC and not Webforms. have to convert your application to use this.

  • @Gutoschiavon you could even set up routes in your Web Forms, but believe me, it’s not worth the effort. I strongly recommend the adoption of the MVC standard, along with other native frameworks that will help you greatly in your tasks (especially the Entity Framework, initially linked to MVC).

  • Is your project still in its infancy? If so, it is worth the effort to adapt to MVC!

  • Yes @Tiagocésaroliveira, my project is at the beginning. I will try the adaptation for MVC. Thanks for the help.

  • @Gutoschiavon have it! If you’re using Visual Studio 2012 or 2013, use version 5 of MVC...

  • @Tiagocésaroliveira is still in version 2010, since it is the version used by the company where I intern.

  • In this case I think you have access to MVC 4 which is also a good

Show 3 more comments

3


If you are using ASP.NET Webforms would have to create a HttpHandler or use the 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 the 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:

inserir a descrição da imagem aqui

ASP.NET Webforms - Form 2 (ASP.NET 3.5 only+)

In the archive Global.asax include:

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

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);
    }
}
  • Excellent answer, @Alexandremarcondes.

  • Thanks @Tiagocésaroliveira. I want to translate the variables of the code, but I’m running with some things here. Soon I must do this.

  • @Alexandremarcondes, how do I edit this for my project? I still don’t understand this Category, action, etc. In my project, using Webforms (3.5+), what would it look like? I need to access example.com/id/123qwe (which would be example.com/id/index.php?id=123qwe) and then get the id '123qwe' in the code. I tried: Routes.Mappageroute(", "id/{id}", "~/index.aspx"); and it was not.

  • @Gutoschiavon what error did you give in this example you tried? Or just doesn’t work without giving error?

  • When trying to access http://localhost:56619/id/123qwe, it says "Unable to find the resource".

  • But @Gutoschiavon, has a record with this id in the bank?

  • Yes, there is. The error of not having registration and not finding the page.

  • You used what technique?

  • I have now: | Routes.Mappageroute("id", "id/{id}", "~/id.aspx", false, new Routevaluedictionary { { "id", "1" } }); |. Now how do I retrieve this ID in my Select, within SQL Data Source?

  • Edit your question puts a separate bar and puts the code you used, please. Add that to the question that will help people a lot and help them understand where you stand. Don’t forget to talk about how you are connecting to a database: Entity framework, ado, etc.

Show 5 more comments

1

.Net 4.0 onwards is already possible to use the MVC routes in project webforms, I myself use. In Global.asax you must register the routes.

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

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

Page.RouteData.Values["locale"]
Page.RouteData.Values["year"]
  • In this case, applying to my project, how would it look? I still can’t quite understand the syntax of the rotations.

  • Add the code that records the route to your aspx page, there in the Global.asax file, in the Start method. Routetable.Routes.Mappageroute("Salesroute" ...

  • @Gutoschiavon See Aquin in my reply how to do: http://answall.com/a/7087/280

Browser other questions tagged

You are not signed in. Login or sign up in order to post.