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.
https://example.com/events/awesome. Seems cleaner.