作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将 URL 从 C# 的 Pascal 格式重写为 SEO 友好格式。
例如,我希望像 /User/Home/MyJumbledPageName
这样的东西看起来像这样:
/user/home/my-jumbled-page-name // lower-case, and words separated by dashes
这是我转换 URL 中每个“ token ”的方法:
public static string GetSEOFriendlyToken(string token)
{
StringBuilder str = new StringBuilder();
for (int i = 0, len = token.Length; i < len; i++)
{
if (i == 0)
{
// setting the first capital char to lower-case:
str.Append(Char.ToLower(token[i]));
}
else if (Char.IsUpper(token[i]))
{
// setting any other capital char to lower-case, preceded by a dash:
str.Append("-" + Char.ToLower(token[i]));
}
else
{
str.Append(token[i]);
}
}
return str.ToString();
}
...在根目录下的 RouteConfig.cs
文件中,我映射了这些路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// without this the first URL is blank:
routes.MapRoute(
name: "Default_Home",
url: "index", // hard-coded?? it works...
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Home",
// the method calls here do not seem to have any effect:
url: GetSEOFriendlyToken("{action}") + "/" + GetSEOFriendlyToken("{id}"),
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
使用此代码,/AboutTheAuthor
等 URL 不会转换为我想要的内容,即 /about-the-author
。
似乎我的方法调用被忽略了,这里发生了什么?实现它的常规方法是什么?
最佳答案
您必须定义自己的RouteBase
类或子类Route
public class SeoFriendlyRoute : Route
{
private readonly string[] _valuesToSeo;
public SeoFriendlyRoute(string url, RouteValueDictionary defaults, IEnumerable<string> valuesToSeo, RouteValueDictionary constraints = null, RouteValueDictionary dataTokens = null, IRouteHandler routeHandler = null)
: base(url, defaults, constraints ?? new RouteValueDictionary(), dataTokens ?? new RouteValueDictionary(), routeHandler ?? new MvcRouteHandler())
{
if (valuesToSeo == null) { throw new ArgumentNullException("valuesToSeo"); }
_valuesToSeo = valuesToSeo.ToArray();
}
public override RouteData GetRouteData(HttpContextBase httpContext)
{
var routeData = base.GetRouteData(httpContext);
if (routeData != null)
{
foreach (var key in _valuesToSeo)
{
if (routeData.Values.ContainsKey(key))
{
routeData.Values[key] = GetActualValue((string)routeData.Values[key]);
}
}
}
return routeData;
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
var seoFriendyValues = new RouteValueDictionary(values);
foreach (var key in _valuesToSeo)
{
if (seoFriendyValues.ContainsKey(key))
{
seoFriendyValues[key] = GetSeoFriendlyValue((string)seoFriendyValues[key]);
}
}
return base.GetVirtualPath(requestContext, seoFriendyValues);
}
private string GetSeoFriendlyValue(string actualValue)
{
//your method
StringBuilder str = new StringBuilder();
for (int i = 0, len = actualValue.Length; i < len; i++)
{
if (i == 0)
{
str.Append(Char.ToLower(actualValue[i]));
}
else if (Char.IsUpper(actualValue[i]))
{
str.Append("-" + Char.ToLower(actualValue[i]));
}
else
{
str.Append(actualValue[i]);
}
}
return str.ToString();
}
private static string GetActualValue(string seoFriendlyValue)
{
//action name is not case sensitive
//one limitation is the dash can be anywhere but the action will still be resolved
// /my-jumbled-page-name is same as /myjumbled-pagename
return seoFriendlyValue.Replace("-", string.Empty);
}
}
用法
routes.Add("Default", new SeoFriendlyRoute(
url: "{controller}/{action}/{id}",
valuesToSeo: new string[] { "action", "controller" },
defaults: new RouteValueDictionary(new { controller = "Home", action = "Index", id = UrlParameter.Optional }))
);
关于c# - 我通常如何在 MapRoute 方法中实现 URL 重写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20651661/
我是一名优秀的程序员,十分优秀!