Jesper
Jesper

Reputation: 999

asp.net c# app routing

I've been trying to get some routing up and running. I basically want this url:

www.example.com/SomebodysName

or

www.example.com/agents/somebodysname

..to go to...

www.example.com/portfolio.aspx?ran=somebodysname

I have tried to use an example from MSDN, using globax.asax like this:

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

    }

public static void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute("", "agents/{name}", "portfolio.aspx?ran={name}");
    }

but I can't get it to work, it says Routing does not exist in the namespace System.Web. how can I get it to work this way or perhaps another way (web.config?)

Upvotes: 0

Views: 1594

Answers (1)

p.campbell
p.campbell

Reputation: 100567

Ensure your Global.asax has a using statement for:

using System.Web.Routing;

You can map your route like this without the param on the target physical aspx page:

routes.MapPageRoute("AgentPortfolioByName", "agents/{name}", "portfolio.aspx");

In the code-behind in portfolio.aspx.cs, you can simply refer to the name value like this:

 string name = Page.RouteData.Values["name"].ToString();

This will ensure your ASP.NET 4+ site will have the URL routing working as you expect/describe.

Upvotes: 1

Related Questions