从visual studio 2008 C#中的url路由中删除%20

本文关键字:路由 删除 url 中的 visual studio 2008 | 更新日期: 2023-09-27 17:57:27

如何从url中删除"%20"?

http://myweb.com/India%20is%20a%20great%20country

我想要喜欢下面的网址。。。。。

http://myweb.com/India-is-a-great-country

下面是我的代码:用于url路由。

public class PostRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        string sub = requestContext.RouteData.Values["sub"] as string;

        if (string.IsNullOrEmpty(sub))
            return Helpers.GetNotFoundHttpHandler();
        else
        {
            if (sub == null)
                return Helpers.GetNotFoundHttpHandler();
            else
            {

                HttpContext.Current.Server.UrlEncode(sub);
                return BuildManager.CreateInstanceFromVirtualPath("~/ViewEntry.aspx",  typeof(Page)) as Page;
            }
        }
    }
}

public class Helpers
{
    public static string FormatProductUrl(string sub)
    {
        return RouteTable.Routes.GetVirtualPath(null, "View Product", new RouteValueDictionary { { "sub", sub } }).VirtualPath;
}

    public static IHttpHandler GetNotFoundHttpHandler()
    {
        return BuildManager.CreateInstanceFromVirtualPath("~/NotFound.aspx", typeof(Page)) as Page;
    }
}

<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<script runat="server">
void Application_Start(object sender, EventArgs e)
{
    RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
    string pathAndQuery = Request.Url.PathAndQuery.ToString().ToLower();
    // Register a route for Products/{ProductName}
    routes.Add(
        "View Product",
        new Route("{sub}", new WebApplication1.PostRouteHandler())
    );
}

下面是链接:

 <h3 style=" padding-bottom:7px"> <asp:HyperLink runat="server" ID="lnkProduct"
                NavigateUrl='<%# Helpers.FormatProductUrl(Eval("sub").ToString()) %>' Text='<%# Eval("sub") %>'>
                </asp:HyperLink>     </h3>

我使用的是visual studio 2008的3.5版本…

从visual studio 2008 C#中的url路由中删除%20

对其使用HttpContext.Current.Server.UrlDecode(sub);