【问题标题】:Is there any easy way using Asp.net Routing to remove .aspx extension using Asp.net Routing有没有使用 Asp.net Routing 使用 Asp.net Routing 删除 .aspx 扩展名的简单方法
【发布时间】:2014-10-12 11:11:52
【问题描述】:

我正在使用 Asp.Net 4.0,为了生成 SEO 友好的 URL,我使用 Asp.Net 路由方法,例如;

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add("BikeSaleRoute", new Route
    (
       "bikes/sale",
       new CustomRouteHandler("~/Contoso/Products/Details.aspx")
    ));
}

在 Application_Start 方法中处理它

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

 }

我正在尝试编写的是一种使用 Asp.net Routing 的方法,它可以从我的所有页面中删除 .aspx 扩展名,而不管它们的目录级别(甚至起始页面 Default.aspx)。请问有这方面的帮助吗?

【问题讨论】:

标签: c# asp.net url-rewriting url-routing


【解决方案1】:

您可以在 IIS 或 Web.Config 中使用 URL Re-writing 并且可以通过以下代码 sn-p 来完成,将以下代码放在“Global.asax”中。

public static void RegisterRoutes(RouteCollection routeCollection) { routeCollection.MapPageRoute("RouteForDefault", "Default", "~/Default.aspx"); }

祝你好运

【讨论】:

  • 使用 www.example.com/Default 而不是使用这个 URL:www.example.com/Default.aspx,它会起作用。
  • 是的,现在可以使用,但这仅适用于一页。如果我们想要一个适用于项目中每个页面的通用方法怎么办?
  • 亲爱的,你可以在很多页面上使用这个方法,比如: routeCollection.MapPageRoute("RouteForDefault", "Page1", "~/Page1.aspx"); routeCollection.MapPageRoute("RouteForDefault", "Page2", "~/Page2.aspx");,没有写单行删除.aspx扩展名的快捷方式,如果你将使用标准的URL重写,再次你会为不同的页面写不同的内容。希望你能理解。谢谢
  • 感谢您的帮助。我尝试搜索替代方案,但除了为每个页面使用单独的规则外,在“路由”的边界下找不到一个。
  • 是的,这样就可以开始了,然后开始使用 IIS URL 重写。祝你好运
【解决方案2】:

如果您想要项目中所有页面的通用解决方案,您可以在此处使用该包: http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx

否则,您可以在 Global.asax 中为应用程序中的每个页面使用它:

public class Global : System.Web.HttpApplication
{
        void RegisterRoutes(RouteCollection routes)
        {
            routes.MapPageRoute("InteriorServices", "InteriorServices", "~/InteriorServices.aspx");
        }
        protected void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes(RouteTable.Routes);
        }
 }

【讨论】:

  • 对于您提供的上述代码 - 这仅适用于特定页面。需要可以适用于解决方案中每个页面的东西。谢谢
猜你喜欢
  • 2011-04-02
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-30
  • 2011-01-11
相关资源
最近更新 更多