asp.net表单中的友好URL

时间:2018-03-09 06:25:47

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

我希望我的网址在asp.net表单中     的 http://www.somesite.com/productid     代替      的 http://www.somesite.com/product/productid     使用asp.net表单。

1 个答案:

答案 0 :(得分:0)

您必须使用称为路由的概念来实现该任务,因此您需要一个Global.asax文件。以下是一个非常基本的代码,当然可以使用相同的想法,您可以为任何页面进行路由。

从Add>添加Global.asax文件;新商品> Global.asax中

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;

namespace YOUR_NAMESPACE
{
    public class Global : System.Web.HttpApplication
    {

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

        private void RegisterRoutes(RouteCollection routes)
        {
            // to avoid considering files having extension other than .aspx
            routes.Ignore("{*allaspx}", new { allaspx = @".*\.aspx(/.*)?" });
            routes.Ignore("{resource}.axd/{*pathInfo}");
            routes.Ignore("{*allcss}",  new { allcss = @".*\.css(/.*)?" });
            routes.Ignore("{*alljpg}",  new { alljpg = @".*\.jpg(/.*)?" });
            routes.Ignore("{*allpng}",  new { allpng = @".*\.png(/.*)?" });
            routes.Ignore("{*allpdf}",  new { allpdf = @".*\.pdf(/.*)?" });
            routes.Ignore("{*alldoc}",  new { alldoc = @".*\.doc(/.*)?" });
            routes.Ignore("{*alljs}",   new { alljs = @".*\.js(/.*)?" });

            // first parameter: your route name to work within your code
            // second parameter: your friendly url is "product/{id}" you can rename it if
            // third parameter: your page name and location
            routes.MapPageRoute("Product-Detail", "product/{id}", "~/product.aspx");
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }
    }
}

您的RoutingHelper.cs文件,以便在代码

中获取友好的URL
using System;
using System.Web;
using System.Web.UI;

namespace YOUR_NAMESPACE
{
    public class RoutingHelpers
    {
        public static string GetProductRouteURL(int id)
        {
            var page = HttpContext.Current.Handler as Page;
            return page.GetRouteUrl("Product-Detail", new { id = id });
        }
    }
}

然后您可以在项目的任何位置使用此URL

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Project.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">

    <title>Default</title>

</head>
<body>

    <form id="mainForm" runat="server">
        <!-- 100 is your product id -->
        Go to Product <a href="<%= RoutingHelpers.GetProductRouteURL(100) %>">Product</a>

    </form>        

</body>
</html>

注意

你的路线名称就在这个例子中#34;产品详情&#34;在RoutingHelper文件和Global.asax文件中必须相同 您可以将RoutingHelper类命名为

希望它可以帮助你