Tabassum Siddiqui
Tabassum Siddiqui

Reputation: 1

Troubleshooting ASP.NET Web Forms Routing and ScriptResource Issues

I am using ASP.NET web forms and have implemented routing for my website. I have a page named profile.aspx where I'm displaying user details. The current URL structure is http://localhost:56386/profile.aspx?username, but I would like it to work as http://localhost:56386/username to match the profile page.

This configuration works for the profile page routing, but it causes issues with other pages. I receive errors in the console related to ScriptResource.axd and my validations stop working.

Errors:

//

VM83:1 Uncaught ReferenceError: WebForm_DoPostBackWithOptions is not defined

Uncaught SyntaxError: Unexpected token '<' (at WebResource.axd?d= getting this error when going to line number the code is

I've been struggling with this for a month, and any help or alternative suggestions to achieve the desired result would be greatly appreciated.

To achieve this, I added the following code to global.asax:

void Application_Start(object sender, EventArgs e)
{
    RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}

And created a RouteConfig class in the App_Start folder as follows:

using System;
using System.Web;
using System.Web.Routing;


namespace ASP
{
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
             routes.Add("Profile", new Route("{id}", new PageRouteHandler("~/profile.aspx")));
        }
    }
}

Edit:

Add this line routes.Ignore("{resource}.axd/{*pathInfo}"); to RouteConfig will fix this error


public static void RegisterRoutes(RouteCollection routes)
        {
            routes.Ignore("{resource}.axd/{*pathInfo}");
            routes.Add("Profile", new Route("{id}", new PageRouteHandler("~/profile.aspx")));
        }

Upvotes: 0

Views: 87

Answers (1)

Albert D. Kallal
Albert D. Kallal

Reputation: 49029

Hum, why bother with any routing at all?

I mean, say we have a web page MyProjects.aspx.

With friendly URLs, then we can have this:

     MyProjects/QuoteNum/12345

or

     MyProjects/InvoiceNum/888834

Or

     MyProjects/Quotes/City/New York/InvoiceNum/12345

So, in above, the web page job can be to display a given project. Thus, with the above friendly URL’s, we can have ANY kind of URL we want to cook up.

Keep in mind that any "/" after the base page works VERY much like URL query parameters. The only difference is you don't require those messy URL query parameters like this:

   MyProjects.aspx?InvoiceNum='12345'&City = 'New York'  

in fact, want a REALLY great example of using so called friendly URL's?

Why of course THIS VERY post on SO. Look at your URL for this question. You see this:

https://stackoverflow.com/questions/76869231

So, in place of using URL query parameters, then your code can do this:

        Dim fURL As IList(Of String)
        fURL = Request.GetFriendlyUrlSegments

        If fURL.Count = 2 Then

            Dim cUser As clsUser = GetUserInfo()

            Select Case fURL(0)
                Case "Quote"
                    ' code here to load a quote
                    If Len(fURL(1)) = 6 AndAlso IsNumeric(fURL(1)) Then
                            Response.Redirect("~/portal/myprojects")
                        End If
                    End If
                    ' code here to load a quote
                Case "InvoiceNum"
                    ' Code here to load a quote


            End Select

        End If

So, this works like URL query parameters. (However, you don’t use ParseQueryString, or Query String, but use the GetFriendlyUrlSegments)

In most cases, a given page will display something such as a project.

Thus, you can have nice looking URL's, one's that users can share. And in fact, they are so easy to read and look like a folder path to end users? Then often users will actually type in the URL. (URL's query parameters are difficult).

So, anything that follows the "/" can thus be a "fake" URL.

I suppose in some cases you could jump or re-route to another page, but I find that requirement rather rare.

I find adopting the above simple approach and using friendly URLs not only cleans up your URL's, but vast improves the user experience. The user does not need (nor see) the .aspx extension, and from their point of view, such URL's become much like a folder path.

  www.mysite.com/Projects/Quote/1343455

  www.mysite.com/Projects/Invoice/134343

So, in above, there is no re-routing required. The target page (Projects) can thus process the extra values much like URL' query parameters, but as above shows, they are far more readable and cleaner. As noted, this VERY page on SO shows a great example of this in action. (SO was written in .net if you wondering).

Upvotes: 0

Related Questions