简体   繁体   English

ASP.NET 4.0中的URL路由

[英]URL Routing in ASP.NET 4.0

I have an app that has 1 aspx and it loads various ascx user controls. 我有一个具有1 aspx的应用程序,它会加载各种ascx用户控件。 This page then forms urls such as page.aspx?section=product&prodid=123 to then access a product etc. 然后,该页面形成诸如page.aspx?section=product&prodid=123 URL,然后访问产品等。

I have been tasked to change this so it uses clean urls. 我受命更改此设置,因此它使用干净的URL。 So my plan is to get page.aspx/product/NAME and then that will load the product. 所以我的计划是获取page.aspx/product/NAME ,然后将加载产品。

I have 3 main routes: 我有3条主要路线:

Product ProductList Content 产品ProductList内容

So. 所以。 In my global.asax I have: 在我的global.asax我有:

 void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        RegisterRoutes(RouteTable.Routes);
    }

 public static void RegisterRoutes(RouteCollection routeCollection)
    {
        routeCollection.MapPageRoute("RouteForProduct", "Product/{ProductName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "ProductName", String.Empty } });
        routeCollection.MapPageRoute("RouteForProductList", "ProductList/{CatName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "CatName", String.Empty } });
        routeCollection.MapPageRoute("RouteForContentList", "Content/{PageName}", "~/IRShop.aspx", false, new RouteValueDictionary { { "PageName", String.Empty } });
    }

I have a using System.Web.Routing as well. 我也有使用System.Web.Routing。

In my page.aspx . 在我的page.aspx C# code, I have initialy tried to do: C#代码,我已经开始尝试这样做:

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

But when I go to URL page.aspx/Product/TestProduct and step through the code, Page.RouteData is always null . 但是,当我转到URL page.aspx/Product/TestProduct并逐步查看代码时, Page.RouteData始终为null I know I need more to get my page loading, but from what I have read, I would have thought this would have been enough to at least let me see how my routing is working. 我知道我需要更多来加载页面,但是从我读到的内容来看,我本以为这足以至少让我看看我的路由是如何工作的。

My reference was: http://www.dotnetjalps.com/2011/12/easy-url-rewriting-in-aspnet-40-web.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FDotNetJalps+%28DotNetJalps%29&utm_content=Google+Reader 我的参考是: http : //www.dotnetjalps.com/2011/12/easy-url-rewriting-in-aspnet-40-web.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+blogspot%2FDotNetJalps+%28DotNetJalps% 29的utm_content =谷歌+读卡器

尝试转到URL“ / Product / ProductName”而不是“ /page.aspx/Product/ProductName”

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

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