1

I have a web forms application that has a few basic pages. I've created routes to make sure those pages don't need .aspx at the end.

routes.MapPageRoute("About", "About", "~/About.aspx");
routes.MapPageRoute("Contact", "Contact", "~/Contact.aspx");
routes.MapPageRoute("faq", "faq", "~/faqs.aspx");
routes.MapPageRoute("Donation", "Donation", "~/Donation.aspx");

I have a sql server database that contains data regarding events that we are holding. This table holds simple values like eventID, location, date, our goal, etc.

Currently I only have one even in my database so I just created a new aspx page with the name of the event. So the url looks like this https://example.com/awesome-event

And I added the appropriate route

routes.MapPageRoute("Awesome-Event", "Awesome-Event", "~/Awesome-Event.aspx");

This works fine, but I want the ability to add new events without having to worry about adding new aspx pages every time. I figured I can use URL rewrite for this. What I want to achieve is to have a single .aspx page that is passed a query string but keep nice looking URLS.

So what I did was create a new aspx page named simply event.aspx

routes.MapPageRoute("Awesome-Event1", "Awesome-Event1", "~/event.aspx?id=1");
routes.MapPageRoute("Awesome-Event2", "Awesome-Event2", "~/event.aspx?id=2");

And in my code behind for the event.aspx page looks like this

protected void Page_Load(object sender, EventArgs e)
{
    string query = Request.QueryString["id"];
    if(query != null)
    {
        PageLable.Text = query.ToString();
    }
}

So currently typing in http://localhost:51197/Awesome-Event1 and http://localhost:51197/Awesome-Event2 both take me to me to the event.aspx page (I can tell because I hit a break point) but it doesn't actually pass the query string of ID.

Am I going about this the right way? Is there a better solution. From my understanding, MVC makes this kind of thing easy but I don't really have much experience with it. The database is already set up and my project is a webforms application so I don't even know if adding MVC to my current project is possible.

4
  • My advice is, use the right tool for the job. This is exactly the scenario that MVC is designed to handle. By the time you've figured out how to hack it onto your existing project, you could follow one of the beginner tutorials in ASP.NET MVC and get it up and running, saving you a lot of maintenance headaches in the future. You can build MVC solutions against an existing DB, just search for "ASP.NET MVC database first" Commented May 2, 2017 at 12:50
  • stackoverflow.com/questions/4938028/… Commented May 2, 2017 at 12:54
  • 2
    @Daniel Routing is perfectly fine in webforms (Routing is only a small part of MVC) - there's even an in-depth MSDN article about it. onTheInternet - I don't have time for a full answer now, but take a read through that article, you should be able to use route parameters. Commented May 2, 2017 at 12:55
  • I would just construct my URL's more like https://example.com/events/awesome. Seems cleaner. Commented May 2, 2017 at 13:45

1 Answer 1

4

Take a look at the MapPageRoute() documentation:

https://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.mappageroute.aspx

It has this example:

routes.MapPageRoute("SalesSummaryRoute",
    "SalesReportSummary/{locale}", "~/sales.aspx");

which you can adapt for your event page like this:

routes.MapPageRoute("Awesome-Event",
    "Awesome-Event/{id}/", "~/event.aspx");

That will allow you to use a url like this:

https://example.com/Awesome-Event/2/

And then you can see which event to render in your event.apsx page like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.RouteData.Values["id"] != null)
    {
        int eventID = Convert.ToInt32(Page.RouteData.Values["id"]);
    }
}

If you want to get fancy, you can do something like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (Page.RouteData.Values["id"] != null)
    {
        int eventID;
        if (int.TryParse(Page.RouteData.Values["id"], out eventID))
        {
           //render event based on numeric ID
        }
        else
        {
           //lookup event based on a string title. Then you could use a url like:
           //https://example.com/Awesome-Event/SuperbowlLII
        }
    }
}

But be careful if you allow both an ID and a title reference the data information, or you could effectively split the search engine ranking for content across both pages. You'll need to specify a canonical url in your html so only one of the possible addresses for an event gets all the credit with search engines.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.