Piotr Kula
Piotr Kula

Reputation: 9811

ASP WebForms Routing

I have set up a routing in global.asax which works fine to a single route destination. like home.aspx

But what I actually am trying to do is route ALL pages after the defined route as normal

mysite.com/token/home.aspx?demo=yes

Click a link to Contact?target=main us goes to

mysite.com/token/contact.aspx?target=main

Because I would always want to use {token} on all the pages but I want the website to operate as usual

At the moment i click a link like contact and the URL get populated as mysite.com/token/contact.aspx?target=main in the URL bar but the server says the resource cannot be found; I assume at server level its routing to /home.aspx/contact.aspx?target=main

Is there a wildcard or a setting to route as normal any other.. or all pages to the actual requested page and not to a static route-- but still be able to access the token as a route variable instead of a parameter on the queries?

For clarification this is ASP WEB FORMS on .NET4 not MVC

Upvotes: 1

Views: 126

Answers (1)

Per Stenmar
Per Stenmar

Reputation: 26

You can define a wildcard route and use the BuildManager to return the correct file.

Example:

public class TokenRoute : Route 
{
    class TokenRouteHandler : IRouteHandler
    {
        #region IRouteHandler Members

        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            var path = Convert.ToString(requestContext.RouteData.Values["path"]);
            if (string.IsNullOrEmpty(path))
            {
                path = "Default.aspx";
            }

            var vPath = "~/" + path;

            HttpContext.Current.Items[RoutingUtil.UrlRoutingVirtualPathKey] = vPath;

            return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(vPath, typeof(IHttpHandler));
        }

        #endregion
    }

    public TokenRoute(string token) 
           : base(token + "/{*path}", null, new RouteValueDictionary(), new TokenRouteHandler())
    {
        DataTokens = new RouteValueDictionary { { "token", token } };
    }
}

hope that can help!

Upvotes: 1

Related Questions