-2

I Need your Help Please; I'm developing a asp.net web application using c# I used Microsoft AspNet FriendlyUrls to make url without the suffix aspx in App_Code>App_start I added the following class

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings,new WebMethodFriendlyUrlResolver());
    }

    public class WebMethodFriendlyUrlResolver : WebFormsFriendlyUrlResolver
    {
        public override string ConvertToFriendlyUrl(string path)
        {
            if (HttpContext.Current.Request.PathInfo != string.Empty)
            {
                return path;
            }
            else
            {
                return base.ConvertToFriendlyUrl(path);
            }
        }
    }
}

in Global.asax i used the following code

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup

        RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
        
    }

it's working fine for dropping aspx part from url NOW.......... I have a search page uses 3 parameters in url , as a example (Plumber,Cairo,Dar Elsalam)

.../search/find?Syx=job=Plumber%20+%20state=CAIRO%20+%20city=ELSALAM%20DAR%20+%20

How to make Up URL become

.../search/plumber-in-dar-elsalam

I Would be appreciated to Your ideas

i searched over internet for a solution but couldn't find a suitable solution to my case

I tried this code

routes.MapPageRoute("plum-darelsalam", "plumber-in-dar-elsalam", "~/find?Syx=job=Plumber%20+%20state=CAIRO%20+%20city=ELSALAM%20DAR%20+%20.aspx");

after

routes.EnableFriendlyUrls(settings,new WebMethodFriendlyUrlResolver());

But didn’t work ☹️

Mohamed
  • 1
  • 2
  • How would it know that `"job=Plumber + state=CAIRO + city=ELSALAM DAR + "` should become `"plumber-in-dar-elsalam"` How did you get "in"? What happened to "CAIRO"? How did "ELSALAM DAR" become "dar-elsalam"? Redirecting on its own is simple, but this additional semantic conversion is something you have to figure out separately, first. Focus on that. – madreflection Apr 24 '23 at 19:37
  • @madreflection thanks for your comment; If it can’t be done dynamically, is there’s a way to convert "job=Plumber + state=CAIRO + city=ELSALAM DAR + " To "plumber-in-dar-elsalam" ? – Mohamed Apr 24 '23 at 19:57
  • Sure, maybe, but you're probably going to have to write it yourself. It seems like it's very specific to your use case. If you're looking for something pre-packaged, I'd say you're not going to find it. – madreflection Apr 24 '23 at 20:03

1 Answers1

0

Well, with this URL
/search/findSyx=job=Plumber%20+%20state=CAIRO%20+%20city=ELSALAM%20DAR%20+%20

../search/find?Syx=job=Plumber%20+%20state=CAIRO%20+%20city=ELSALAM%20DAR%20+%20

As a friendly URL, it would become:

/search/job/Plumber/Cario/Elalam dar

(or at least close to above).

so for say
/Projects?QuoteNum=123456

You can have:

 /Projects/QuoteNum/123456

or even

 /Projects/Quotes/123456

Note that in above, assuming the base page is "projects", then you can make some nice links quite much anyway you want. You could even have this:

 /Projects/123456

However, it "most often" better to include what the "thing" is, so

  /Projects/Invoices/123456

Note that the "extra" values or key words you decide to have is 100% up to you.

NOW THE WHOPPER part!!!!

.net does not automatic CHANGE the old style URL's you have, nor will the NEW format work my some act of magic. YOU the developer NOW have to grab and use these new values.

So, code will look like this:

for the page Projects.aspx, as per above examples, then you can do this in code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            // first page load, get friendly URL parts
            IList<string> fURL = new List<string>();
            fURL = Request.GetFriendlyUrlSegments();    

            if (fURL.Count == 2)
            {
                // 2 values in URL, 

                switch (fURL[0])
                {
                    case "Quote":
                        string sQuoteNumber = fURL[1];
                        // code here for getting quote number
                        break;

                    case "Invoice":
                        string sInvoiceNumber = fURL[2];
                        // code here for invoice number
                        break;
                }
            }

So, there is NOT a automatic conversion of "old style" parameter's to NEW style, you have to write the code to use and deal with the new style of URL parameter's. (this will require new code on your part).

So, in above, you can say use this:

 Projects/Quotes/123456

or

 Projects/Invoices/2345

And REALLY nice of course is users don't think of parameter's, but will not only see a nice clean link, but one that looks like a "folder", and they can ALSO share such links.

And while users can "mess" with parameter's, they are hard, but friendly URL's are so nice, they often will start to use them to get to a given quote, or even share such links.

So, as old style URL with parameter's, you might have this:

  Projects?State="New York"&Quote=12345

The above now becomes

 Projects/New York/12345

Or maybe

 Projects/New York/Quotes/12345

So, you can make really nice URL's as a result, but YOU the developer WILL THEN have to get/grab/process/use/write code using the GetFriendlyUrlSegments, and there is not some automatic, or auto magic conversion that occurs for you.

You can certainly support both types of URL parameter's. That will mean you have to keep your existing old style code, and also THEN add additional new code for the new type of parameter's - they are not "one to one" or interchangeable.

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51