简体   繁体   中英

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?)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM