如何從url中刪除「%20」?下面從Visual Studio 2008的URL路由中刪除%20 C#
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」,「 - 」 –