简体   繁体   English

ASP.NET C#应用程序路由

[英]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 www.example.com/SomebodysName

or 要么

www.example.com/agents/somebodysname www.example.com/agents/somebodysname

..to go to... ..要去...

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

I have tried to use an example from MSDN, using globax.asax like this: 我试图使用来自MSDN的示例,使用globax.asax像这样:

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. 但我无法使其正常工作,它说路由在命名空间System.Web中不存在。 how can I get it to work this way or perhaps another way (web.config?) 如何使它以这种方式或另一种方式工作(web.config?)

Ensure your Global.asax has a using statement for: 确保您的Global.asax在以下方面具有using语句:

using System.Web.Routing;

You can map your route like this without the param on the target physical aspx page: 您可以这样映射路线,而无需在目标物理aspx页面上设置参数:

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: 在Portfolio.aspx.cs的代码隐藏中,您可以像这样简单地引用name值:

 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. 这将确保您的ASP.NET 4+站点将按照您的期望/描述进行URL路由。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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