0

I'm learning ASP.NET 4.5 and stumbled upon this post about Friendly URLs. Now I'm using ASP.NET 4.5 and it turns out the feature was already enabled and installed when I create a new ASP.NET project.

Let's say I have the following URL:

http://somesite.com/Admin/Users/1/2/3

This should map to the following:

http://somesite.com/Admin/Users.aspx?a=1&b=2&c=3

This works fine inside of Users.aspx.cs, but can I use it in Global.asax as well? I've tried it and the following methods return empty strings:

string a = Request.GetFriendlyUrlFileExtension();
string b = Request.GetFriendlyUrlFileVirtualPath();
IList<string> c = Request.GetFriendlyUrlSegments();
Kamil Budziewski
  • 22,699
  • 14
  • 85
  • 105
rad
  • 141
  • 8

2 Answers2

0

I'm not familiar with the friendlyURL package your'e referring to, but if you'd like to try it yourself here is how you could go about writing some custom routes:

In your global.asax

using System.Web.Routing;

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

    }

    void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("Users", "Users/{*queryvalues}", "~/Users.aspx");
    }

then in your Users.aspx.cs instead of getting values from

    Request.QueryString["somekey"]

you would get your values from

    //This makes an array of string values
    RouteData.Values["queryvalues"].ToString().Split('/')

microsoft has an example here

Mark F
  • 176
  • 7
  • The tutorial I read didn't tell me to setup any routes, and the methods did work as expected in `Users.aspx.cs`. However what I wanted to do was to use those methods in `Global.asax.cs`, and unfortunately they all return empty strings. – rad Oct 11 '13 at 03:39
0

I use friendly urls all the time, here is what I do in my web forms project:

Global ASAX

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapPageRoute("About", "About", "~/About.aspx", true);
        routes.MapPageRoute("Add User", "Add User", "~/Add.aspx", true);
        routes.MapPageRoute("Login", "Login", "~/Login.aspx", true);
        routes.MapPageRoute("Map", "Map", "~/Map.aspx", true);
        routes.MapPageRoute("Register", "Register", "~/Register.aspx", true);
        routes.MapPageRoute(
                                       "ViewList",
                                       "ViewList/{c}/{s}", 
                                       "~/ViewList.aspx", 
                                       true
                                     );
        routes.MapPageRoute(
                                       "ViewUser",
                                       "ViewUser/{id}",
                                       "~/ViewUser.aspx",
                                       true
                                     );
        routes.MapPageRoute(
                                     "MyAccount",
                                     "MyAccount/{id}",
                                     "~/MyAccount.aspx",
                                     true
                                   );
    }

Page receiving querystrings

string city = RouteData.Values["c"].ToString();
string state = RouteData.Values["s"].ToString();

You dont need to split querystring values in this configuration.

Sam Cromer
  • 687
  • 3
  • 12
  • 31
  • Yes, unfortunately that's not what I was asking. I tried to use the methods I asked about in `Users.aspx.cs`, and they work as expected. I try them in `Global.asax.cs`, and everything returns an empty string. – rad Oct 11 '13 at 03:36
  • I guess I dont follow why you would need or want to do this in GLobal? – Sam Cromer Oct 11 '13 at 13:49
  • I'm trying to build an authorization method that depends on path matching, backed by a set of tables in the database. That said, I need to know the exact path the user requested and filter off all the segments that should be parameters; and I need to do this in `Global.asax.cs`. I'm open to other ideas so please fire away if you have one. – rad Oct 12 '13 at 02:33