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
或
www.example.com/agents/somebodysname
www.example.com/agents/somebodysname
..去...
www.example.com/portfolio.aspx?ran=somebodysname
www.example.com/portfolio.aspx?ran=somebodysname
我尝试使用MSDN的示例,使用globax.asax :
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}");
}
但我无法正常工作,它说路由不存在在名称空间System.Web中。
如何使它以这种方式或另一种方式工作(web.config?)
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?)
推荐答案
确保您的Global.asax在以下情况下具有using语句:
Ensure your Global.asax has a using statement for:
using System.Web.Routing;
您可以在目标物理aspx页面上以不带参数的方式映射这样的路线:
You can map your route like this without the param on the target physical aspx page:
routes.MapPageRoute("AgentPortfolioByName", "agents/{name}", "portfolio.aspx");
在Portfolio.aspx.cs的代码中,您可以简单地引用 name
值,如下所示:
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();
这将确保您的ASP.NET 4+站点将按照您的期望/描述进行URL路由
This will ensure your ASP.NET 4+ site will have the URL routing working as you expect/describe.
这篇关于ASP.NET C#应用程序路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!