【问题标题】:Remove %20 from url routing in visual studio 2008 C#从 Visual Studio 2008 C# 中的 url 路由中删除 %20
【发布时间】:2014-05-22 06:49:30
【问题描述】:

如何从网址中删除“%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>

我正在使用 3.5 版本的 Visual Studio 2008...

【问题讨论】:

  • 您可以将“%20”替换为“-”

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


【解决方案1】:

同样使用HttpContext.Current.Server.UrlDecode(sub);

【讨论】:

  • 在下面的代码中使用它不起作用.. HttpContext.Current.Server.UrlDecode(sub);
  • 替换你的 HttpContext.Current.Server.UrlEncode(sub);与 HttpContext.Current.Server.UrlDecode(sub);并尝试
猜你喜欢
  • 1970-01-01
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-17
  • 1970-01-01
  • 2014-03-07
  • 2019-08-15
相关资源
最近更新 更多