Search code examples
asp.netwebformsurl-routingasp.net-routingwebforms-routing

Why does my ASP.Net 4.8.1 Webform URL still show the query string?


I'm attempting what seems like a trivial set up for friendlier looking URLs with routing. I'm not getting the formatted URL, always the usual one with query strings.

Here is the relevant code for the Global.asax.cs page:

protected void Application_Start(object sender, EventArgs e) {
            RegisterRoutes(RouteTable.Routes);
        }

        void RegisterRoutes(RouteCollection routes) {
            routes.MapPageRoute("product-category", "products/{category}", "~/products.aspx");
        }

And from products.aspx:

public partial class products : Basepage {
        private string category = "";

        protected void Page_Load(object sender, EventArgs e) {
                category = "chocolate";
        }
}

My URL still looks like http://localhost/products.aspx?category=3 when I would like it look like http://localhost/products/chocolate

The debugger successfully enters the RegisterRoutes method.


Solution

  • Well, you have a choice.

    What is the URL you are supplying?

    Adding "friendly" URLs does not OUT OF THE BLUE automatic "fix" or "re-format" existing URLs. So, the older style query parameters in the URL will (and should) still work. I mean you HAVE to add code to parse out friendly URLs if you decide to use them, and EXISTING URLs can still be used, but if you want friendly URL format, then you have to supply additional code in that page to pick apart the new "format" of the URLs in question.

    You can say have this page:

     http://localhost:63267/HotelEditOne2.aspx?HotelName=Super 8
    

    And of course with friendly URLs turned on, we can leave out the .aspx.

    So, now we have this:

    http://localhost:63267/HotelEditOne2?HotelName=Super 8
    

    And the code behind is thus this:

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        void LoadData()
        {
            string sHotelName = "";
    
            if (!string.IsNullOrEmpty(Request.QueryString["HotelName"]))
            {
                sHotelName = Request.QueryString["HotelName"];
            }
    
         // code here to load data based on sHotelName.
    

    However, if you want to use/have/enjoy/adopt friendly URLs: then OK, we can say have this as the URL:

    http://localhost:63267/HotelEditOne2/HotelName/Super 8
    

    Now I REALLY do like those friendly URLs, since they are search engine friendly, but MORE important, you can share such links with others.

    And while users (hardly) ever can type in URLs with parameters, THEY OFTEN can figure out the above, since to end users, this looks like a folder path, and that users get/grasp/know/understand/can learn to use real easy.

    So, let's add the friendly URL ability to above example.

    So, we now have this code:

        protected void Page_Load(object sender, EventArgs e)
        {
            LoadData();
        }
        void LoadData()
        {
            string sHotelName = "";
    
            if (!string.IsNullOrEmpty(Request.QueryString["HotelName"]))
            {
                sHotelName = Request.QueryString["HotelName"];
            }
            IList<string> fURL = new List<string>();
            fURL = Request.GetFriendlyUrlSegments();
    
            if ( (fURL.Count == 2) && (fURL[0] == "HotelName"))
            {
                sHotelName = fURL[1];
            }
    
            if (sHotelName != "")
            {
                string strSQL =
                    @"SELECT * FROM tblHotelsA WHERE HotelName = @Hotel";
    
                SqlCommand cmdSQL = new SqlCommand(strSQL);
                cmdSQL.Parameters.Add("@Hotel", SqlDbType.NVarChar).Value = sHotelName;
    
                DataTable rstHotel = General.MyRstP(cmdSQL);
                General.FLoader(EditRecord, rstHotel.Rows[0]);
            }
    

    So, now I can user either the old style URL "query" parameters, or the newer friendly URLs.

    So, we now get/see this:

    enter image description here

    And I could have EVEN used HotelName in the root, so you can on the fly make up URLs and not even have a web page.

    But, for the most part, I tend to use/have some landing page.

    but, I could even change the above, and say in place of "HotelName", add City, or whatever, and they all look real nice.

    so, say this:

    http://localhost:63267/HotelEditOne2/City/Edmonton
    

    And then display a list of hotels from my City.

    And note how I did not even bother to add "Quotes" around Edmonton.

    So, friendly URLs are great, and in fact THIS VERY web site and message post is based on .net and it also uses this feature. So, the URL to this question is:

     https://stackoverflow.com/questions/75271601
    

    So, while "by default" most templates - even those for webforms by default will have friendly URLs turned on by default, you HAVE to change your existing code to use them, and the "older" style URL "query" parameters don't "automatic" work for friendly URLs, but as above shows, you can certainly support both if say for example you currently have some URLs parameters, and don't want to break existing code.

    But, if you pass older style URLs with parameters, they still have to work, don't they?

    Or maybe you want a friendly URLs but STILL are to use the older style parameters? If you choose that road (friendly + parameters), then in most cases, YOU ONLY get the dropping of the .aspx extension, but you STILL have to provide the parameters.

    So, if you want to adopt that "path like" format for friendly URLs, then you HAVE to change your code, and there is not a "automatic" translate from one format to the other. (ie: the old style URL parameters don't "automatic" get changed to the nice new "path like" format with "/" for drilling down the parameters.

    Thus, as noted, you will as per above have to change your code to support parsing out values from those friendly URLs. You can thus support both formats, but this requires code and efforts on your part, and a "simple" switch on of friendly URLs STILL requires you to write code to enjoy the new parameters base format.

    I do high recommend adopting them, as they really clean up your URLs.

    For page to page navigations, then I use code behind and session to pass values from page to page. I very much dislike parameters in URLs (and they are often not secure at all - so NEVER pass things like say a database PK id).

    However, for customers to jump to a project or whatever by some quote number etc? Then yes, customers take like fish to water in regards to URLs that provide nice "folder like" drill down to a given project, or quote/invoice number or whatever.