c# - Human readable URL Scheme for ASP.NET MVC -


my asp site has database full of events. currently, action on controller linking particular event has url like: "mysite.com/events/5"

this default behavior, , i'm looking how make url more like: "mysite.com/events/some-event-name-that-makes-sense

i have plenty of properties on event entity map url, not sure how apply it, , how allow action find when linking it.

what you're referring called slug. however, can't use old property on model stuff url. typically, you'll have separate property called slug store url-safe version of title or name of object. once you've done this, it's simple matter of using generate urls , objects.

for example, have had like:

url.action("events", new { id = item.id }) 

you'd use instead:

url.action("events", new { slug = item.slug }) 

then, modify action accept slug instead of id , use query event:

public actionresult eventdetail(string slug) {     var evt = db.events.singleordefault(m => m.slug == slug);      ... 

the slight hitch need way generate slug save rest of event data. have following extension methods use:

public static string removediacritics(this string s) {     s = s ?? string.empty;     if (s.length > 0)     {         char[] chars = new char[s.length];         int charindex = 0;          s = s.normalize(normalizationform.formd);         (int = 0; < s.length; i++)         {             char c = s[i];             if (charunicodeinfo.getunicodecategory(c) != unicodecategory.nonspacingmark)                 chars[charindex++] = c;         }          return new string(chars, 0, charindex).normalize(normalizationform.formc);     }      return s; }  public static string slugify(this string s) {     s = s ?? string.empty;     //first lower case     s = s.tolowerinvariant().removediacritics();     //replace spaces     s = regex.replace(s, @"\s", "-", regexoptions.compiled);     //remove invalid chars     s = regex.replace(s, @"[^a-z0-9s\-_]", "", regexoptions.compiled);     //trim dashes end     s = s.trim('-', '_');     //replace double occurences of - or _     s = regex.replace(s, @"([\-_]){2,}", "$1", regexoptions.compiled);      return s; } 

so, then, you'd like:

evt.slug = evt.title.slugify(); 

and/or can make editable field in admin/cms.

update

javascript version

var mynamespace = mynamespace || {}; mynamespace.utilities = {};  mynamespace.utilities.removediacritics = (function () {     var diacritics = { "\u24b6": "a", "\uff21": "a", "\u00c0": "a", "\u00c1": "a", "\u00c2": "a", "\u1ea6": "a", "\u1ea4": "a", "\u1eaa": "a", "\u1ea8": "a", "\u00c3": "a", "\u0100": "a", "\u0102": "a", "\u1eb0": "a", "\u1eae": "a", "\u1eb4": "a", "\u1eb2": "a", "\u0226": "a", "\u01e0": "a", "\u00c4": "a", "\u01de": "a", "\u1ea2": "a", "\u00c5": "a", "\u01fa": "a", "\u01cd": "a", "\u0200": "a", "\u0202": "a", "\u1ea0": "a", "\u1eac": "a", "\u1eb6": "a", "\u1e00": "a", "\u0104": "a", "\u023a": "a", "\u2c6f": "a", "\ua732": "aa", "\u00c6": "ae", "\u01fc": "ae", "\u01e2": "ae", "\ua734": "ao", "\ua736": "au", "\ua738": "av", "\ua73a": "av", "\ua73c": "ay", "\u24b7": "b", "\uff22": "b", "\u1e02": "b", "\u1e04": "b", "\u1e06": "b", "\u0243": "b", "\u0182": "b", "\u0181": "b", "\u24b8": "c", "\uff23": "c", "\u0106": "c", "\u0108": "c", "\u010a": "c", "\u010c": "c", "\u00c7": "c", "\u1e08": "c", "\u0187": "c", "\u023b": "c", "\ua73e": "c", "\u24b9": "d", "\uff24": "d", "\u1e0a": "d", "\u010e": "d", "\u1e0c": "d", "\u1e10": "d", "\u1e12": "d", "\u1e0e": "d", "\u0110": "d", "\u018b": "d", "\u018a": "d", "\u0189": "d", "\ua779": "d", "\u01f1": "dz", "\u01c4": "dz", "\u01f2": "dz", "\u01c5": "dz", "\u24ba": "e", "\uff25": "e", "\u00c8": "e", "\u00c9": "e", "\u00ca": "e", "\u1ec0": "e", "\u1ebe": "e", "\u1ec4": "e", "\u1ec2": "e", "\u1ebc": "e", "\u0112": "e", "\u1e14": "e", "\u1e16": "e", "\u0114": "e", "\u0116": "e", "\u00cb": "e", "\u1eba": "e", "\u011a": "e", "\u0204": "e", "\u0206": "e", "\u1eb8": "e", "\u1ec6": "e", "\u0228": "e", "\u1e1c": "e", "\u0118": "e", "\u1e18": "e", "\u1e1a": "e", "\u0190": "e", "\u018e": "e", "\u24bb": "f", "\uff26": "f", "\u1e1e": "f", "\u0191": "f", "\ua77b": "f", "\u24bc": "g", "\uff27": "g", "\u01f4": "g", "\u011c": "g", "\u1e20": "g", "\u011e": "g", "\u0120": "g", "\u01e6": "g", "\u0122": "g", "\u01e4": "g", "\u0193": "g", "\ua7a0": "g", "\ua77d": "g", "\ua77e": "g", "\u24bd": "h", "\uff28": "h", "\u0124": "h", "\u1e22": "h", "\u1e26": "h", "\u021e": "h", "\u1e24": "h", "\u1e28": "h", "\u1e2a": "h", "\u0126": "h", "\u2c67": "h", "\u2c75": "h", "\ua78d": "h", "\u24be": "i", "\uff29": "i", "\u00cc": "i", "\u00cd": "i", "\u00ce": "i", "\u0128": "i", "\u012a": "i", "\u012c": "i", "\u0130": "i", "\u00cf": "i", "\u1e2e": "i", "\u1ec8": "i", "\u01cf": "i", "\u0208": "i", "\u020a": "i", "\u1eca": "i", "\u012e": "i", "\u1e2c": "i", "\u0197": "i", "\u24bf": "j", "\uff2a": "j", "\u0134": "j", "\u0248": "j", "\u24c0": "k", "\uff2b": "k", "\u1e30": "k", "\u01e8": "k", "\u1e32": "k", "\u0136": "k", "\u1e34": "k", "\u0198": "k", "\u2c69": "k", "\ua740": "k", "\ua742": "k", "\ua744": "k", "\ua7a2": "k", "\u24c1": "l", "\uff2c": "l", "\u013f": "l", "\u0139": "l", "\u013d": "l", "\u1e36": "l", "\u1e38": "l", "\u013b": "l", "\u1e3c": "l", "\u1e3a": "l", "\u0141": "l", "\u023d": "l", "\u2c62": "l", "\u2c60": "l", "\ua748": "l", "\ua746": "l", "\ua780": "l", "\u01c7": "lj", "\u01c8": "lj", "\u24c2": "m", "\uff2d": "m", "\u1e3e": "m", "\u1e40": "m", "\u1e42": "m", "\u2c6e": "m", "\u019c": "m", "\u24c3": "n", "\uff2e": "n", "\u01f8": "n", "\u0143": "n", "\u00d1": "n", "\u1e44": "n", "\u0147": "n", "\u1e46": "n", "\u0145": "n", "\u1e4a": "n", "\u1e48": "n", "\u0220": "n", "\u019d": "n", "\ua790": "n", "\ua7a4": "n", "\u01ca": "nj", "\u01cb": "nj", "\u24c4": "o", "\uff2f": "o", "\u00d2": "o", "\u00d3": "o", "\u00d4": "o", "\u1ed2": "o", "\u1ed0": "o", "\u1ed6": "o", "\u1ed4": "o", "\u00d5": "o", "\u1e4c": "o", "\u022c": "o", "\u1e4e": "o", "\u014c": "o", "\u1e50": "o", "\u1e52": "o", "\u014e": "o", "\u022e": "o", "\u0230": "o", "\u00d6": "o", "\u022a": "o", "\u1ece": "o", "\u0150": "o", "\u01d1": "o", "\u020c": "o", "\u020e": "o", "\u01a0": "o", "\u1edc": "o", "\u1eda": "o", "\u1ee0": "o", "\u1ede": "o", "\u1ee2": "o", "\u1ecc": "o", "\u1ed8": "o", "\u01ea": "o", "\u01ec": "o", "\u00d8": "o", "\u01fe": "o", "\u0186": "o", "\u019f": "o", "\ua74a": "o", "\ua74c": "o", "\u0152": "oe", "\u01a2": "oi", "\ua74e": "oo", "\u0222": "ou", "\u24c5": "p", "\uff30": "p", "\u1e54": "p", "\u1e56": "p", "\u01a4": "p", "\u2c63": "p", "\ua750": "p", "\ua752": "p", "\ua754": "p", "\u24c6": "q", "\uff31": "q", "\ua756": "q", "\ua758": "q", "\u024a": "q", "\u24c7": "r", "\uff32": "r", "\u0154": "r", "\u1e58": "r", "\u0158": "r", "\u0210": "r", "\u0212": "r", "\u1e5a": "r", "\u1e5c": "r", "\u0156": "r", "\u1e5e": "r", "\u024c": "r", "\u2c64": "r", "\ua75a": "r", "\ua7a6": "r", "\ua782": "r", "\u24c8": "s", "\uff33": "s", "\u015a": "s", "\u1e64": "s", "\u015c": "s", "\u1e60": "s", "\u0160": "s", "\u1e66": "s", "\u1e62": "s", "\u1e68": "s", "\u0218": "s", "\u015e": "s", "\u2c7e": "s", "\ua7a8": "s", "\ua784": "s", "\u1e9e": "ss", "\u24c9": "t", "\uff34": "t", "\u1e6a": "t", "\u0164": "t", "\u1e6c": "t", "\u021a": "t", "\u0162": "t", "\u1e70": "t", "\u1e6e": "t", "\u0166": "t", "\u01ac": "t", "\u01ae": "t", "\u023e": "t", "\ua786": "t", "\ua728": "tz", "\u24ca": "u", "\uff35": "u", "\u00d9": "u", "\u00da": "u", "\u00db": "u", "\u0168": "u", "\u1e78": "u", "\u016a": "u", "\u1e7a": "u", "\u016c": "u", "\u00dc": "u", "\u01db": "u", "\u01d7": "u", "\u01d5": "u", "\u01d9": "u", "\u1ee6": "u", "\u016e": "u", "\u0170": "u", "\u01d3": "u", "\u0214": "u", "\u0216": "u", "\u01af": "u", "\u1eea": "u", "\u1ee8": "u", "\u1eee": "u", "\u1eec": "u", "\u1ef0": "u", "\u1ee4": "u", "\u1e72": "u", "\u0172": "u", "\u1e76": "u", "\u1e74": "u", "\u0244": "u", "\u24cb": "v", "\uff36": "v", "\u1e7c": "v", "\u1e7e": "v", "\u01b2": "v", "\ua75e": "v", "\u0245": "v", "\ua760": "vy", "\u24cc": "w", "\uff37": "w", "\u1e80": "w", "\u1e82": "w", "\u0174": "w", "\u1e86": "w", "\u1e84": "w", "\u1e88": "w", "\u2c72": "w", "\u24cd": "x", "\uff38": "x", "\u1e8a": "x", "\u1e8c": "x", "\u24ce": "y", "\uff39": "y", "\u1ef2": "y", "\u00dd": "y", "\u0176": "y", "\u1ef8": "y", "\u0232": "y", "\u1e8e": "y", "\u0178": "y", "\u1ef6": "y", "\u1ef4": "y", "\u01b3": "y", "\u024e": "y", "\u1efe": "y", "\u24cf": "z", "\uff3a": "z", "\u0179": "z", "\u1e90": "z", "\u017b": "z", "\u017d": "z", "\u1e92": "z", "\u1e94": "z", "\u01b5": "z", "\u0224": "z", "\u2c7f": "z", "\u2c6b": "z", "\ua762": "z", "\u24d0": "a", "\uff41": "a", "\u1e9a": "a", "\u00e0": "a", "\u00e1": "a", "\u00e2": "a", "\u1ea7": "a", "\u1ea5": "a", "\u1eab": "a", "\u1ea9": "a", "\u00e3": "a", "\u0101": "a", "\u0103": "a", "\u1eb1": "a", "\u1eaf": "a", "\u1eb5": "a", "\u1eb3": "a", "\u0227": "a", "\u01e1": "a", "\u00e4": "a", "\u01df": "a", "\u1ea3": "a", "\u00e5": "a", "\u01fb": "a", "\u01ce": "a", "\u0201": "a", "\u0203": "a", "\u1ea1": "a", "\u1ead": "a", "\u1eb7": "a", "\u1e01": "a", "\u0105": "a", "\u2c65": "a", "\u0250": "a", "\ua733": "aa", "\u00e6": "ae", "\u01fd": "ae", "\u01e3": "ae", "\ua735": "ao", "\ua737": "au", "\ua739": "av", "\ua73b": "av", "\ua73d": "ay", "\u24d1": "b", "\uff42": "b", "\u1e03": "b", "\u1e05": "b", "\u1e07": "b", "\u0180": "b", "\u0183": "b", "\u0253": "b", "\u24d2": "c", "\uff43": "c", "\u0107": "c", "\u0109": "c", "\u010b": "c", "\u010d": "c", "\u00e7": "c", "\u1e09": "c", "\u0188": "c", "\u023c": "c", "\ua73f": "c", "\u2184": "c", "\u24d3": "d", "\uff44": "d", "\u1e0b": "d", "\u010f": "d", "\u1e0d": "d", "\u1e11": "d", "\u1e13": "d", "\u1e0f": "d", "\u0111": "d", "\u018c": "d", "\u0256": "d", "\u0257": "d", "\ua77a": "d", "\u01f3": "dz", "\u01c6": "dz", "\u24d4": "e", "\uff45": "e", "\u00e8": "e", "\u00e9": "e", "\u00ea": "e", "\u1ec1": "e", "\u1ebf": "e", "\u1ec5": "e", "\u1ec3": "e", "\u1ebd": "e", "\u0113": "e", "\u1e15": "e", "\u1e17": "e", "\u0115": "e", "\u0117": "e", "\u00eb": "e", "\u1ebb": "e", "\u011b": "e", "\u0205": "e", "\u0207": "e", "\u1eb9": "e", "\u1ec7": "e", "\u0229": "e", "\u1e1d": "e", "\u0119": "e", "\u1e19": "e", "\u1e1b": "e", "\u0247": "e", "\u025b": "e", "\u01dd": "e", "\u24d5": "f", "\uff46": "f", "\u1e1f": "f", "\u0192": "f", "\ua77c": "f", "\u24d6": "g", "\uff47": "g", "\u01f5": "g", "\u011d": "g", "\u1e21": "g", "\u011f": "g", "\u0121": "g", "\u01e7": "g", "\u0123": "g", "\u01e5": "g", "\u0260": "g", "\ua7a1": "g", "\u1d79": "g", "\ua77f": "g", "\u24d7": "h", "\uff48": "h", "\u0125": "h", "\u1e23": "h", "\u1e27": "h", "\u021f": "h", "\u1e25": "h", "\u1e29": "h", "\u1e2b": "h", "\u1e96": "h", "\u0127": "h", "\u2c68": "h", "\u2c76": "h", "\u0265": "h", "\u0195": "hv", "\u24d8": "i", "\uff49": "i", "\u00ec": "i", "\u00ed": "i", "\u00ee": "i", "\u0129": "i", "\u012b": "i", "\u012d": "i", "\u00ef": "i", "\u1e2f": "i", "\u1ec9": "i", "\u01d0": "i", "\u0209": "i", "\u020b": "i", "\u1ecb": "i", "\u012f": "i", "\u1e2d": "i", "\u0268": "i", "\u0131": "i", "\u24d9": "j", "\uff4a": "j", "\u0135": "j", "\u01f0": "j", "\u0249": "j", "\u24da": "k", "\uff4b": "k", "\u1e31": "k", "\u01e9": "k", "\u1e33": "k", "\u0137": "k", "\u1e35": "k", "\u0199": "k", "\u2c6a": "k", "\ua741": "k", "\ua743": "k", "\ua745": "k", "\ua7a3": "k", "\u24db": "l", "\uff4c": "l", "\u0140": "l", "\u013a": "l", "\u013e": "l", "\u1e37": "l", "\u1e39": "l", "\u013c": "l", "\u1e3d": "l", "\u1e3b": "l", "\u0142": "l", "\u019a": "l", "\u026b": "l", "\u2c61": "l", "\ua749": "l", "\ua781": "l", "\ua747": "l", "\u01c9": "lj", "\u24dc": "m", "\uff4d": "m", "\u1e3f": "m", "\u1e41": "m", "\u1e43": "m", "\u0271": "m", "\u026f": "m", "\u24dd": "n", "\uff4e": "n", "\u01f9": "n", "\u0144": "n", "\u00f1": "n", "\u1e45": "n", "\u0148": "n", "\u1e47": "n", "\u0146": "n", "\u1e4b": "n", "\u1e49": "n", "\u019e": "n", "\u0272": "n", "\u0149": "n", "\ua791": "n", "\ua7a5": "n", "\u01cc": "nj", "\u24de": "o", "\uff4f": "o", "\u00f2": "o", "\u00f3": "o", "\u00f4": "o", "\u1ed3": "o", "\u1ed1": "o", "\u1ed7": "o", "\u1ed5": "o", "\u00f5": "o", "\u1e4d": "o", "\u022d": "o", "\u1e4f": "o", "\u014d": "o", "\u1e51": "o", "\u1e53": "o", "\u014f": "o", "\u022f": "o", "\u0231": "o", "\u00f6": "o", "\u022b": "o", "\u1ecf": "o", "\u0151": "o", "\u01d2": "o", "\u020d": "o", "\u020f": "o", "\u01a1": "o", "\u1edd": "o", "\u1edb": "o", "\u1ee1": "o", "\u1edf": "o", "\u1ee3": "o", "\u1ecd": "o", "\u1ed9": "o", "\u01eb": "o", "\u01ed": "o", "\u00f8": "o", "\u01ff": "o", "\u0254": "o", "\ua74b": "o", "\ua74d": "o", "\u0275": "o", "\u0153": "oe", "\u0276": "oe", "\u01a3": "oi", "\u0223": "ou", "\ua74f": "oo", "\u24df": "p", "\uff50": "p", "\u1e55": "p", "\u1e57": "p", "\u01a5": "p", "\u1d7d": "p", "\ua751": "p", "\ua753": "p", "\ua755": "p", "\u24e0": "q", "\uff51": "q", "\u024b": "q", "\ua757": "q", "\ua759": "q", "\u24e1": "r", "\uff52": "r", "\u0155": "r", "\u1e59": "r", "\u0159": "r", "\u0211": "r", "\u0213": "r", "\u1e5b": "r", "\u1e5d": "r", "\u0157": "r", "\u1e5f": "r", "\u024d": "r", "\u027d": "r", "\ua75b": "r", "\ua7a7": "r", "\ua783": "r", "\u24e2": "s", "\uff53": "s", "\u015b": "s", "\u1e65": "s", "\u015d": "s", "\u1e61": "s", "\u0161": "s", "\u1e67": "s", "\u1e63": "s", "\u1e69": "s", "\u0219": "s", "\u015f": "s", "\u023f": "s", "\ua7a9": "s", "\ua785": "s", "\u017f": "s", "\u1e9b": "s", "\u00df": "ss", "\u24e3": "t", "\uff54": "t", "\u1e6b": "t", "\u1e97": "t", "\u0165": "t", "\u1e6d": "t", "\u021b": "t", "\u0163": "t", "\u1e71": "t", "\u1e6f": "t", "\u0167": "t", "\u01ad": "t", "\u0288": "t", "\u2c66": "t", "\ua787": "t", "\ua729": "tz", "\u24e4": "u", "\uff55": "u", "\u00f9": "u", "\u00fa": "u", "\u00fb": "u", "\u0169": "u", "\u1e79": "u", "\u016b": "u", "\u1e7b": "u", "\u016d": "u", "\u00fc": "u", "\u01dc": "u", "\u01d8": "u", "\u01d6": "u", "\u01da": "u", "\u1ee7": "u", "\u016f": "u", "\u0171": "u", "\u01d4": "u", "\u0215": "u", "\u0217": "u", "\u01b0": "u", "\u1eeb": "u", "\u1ee9": "u", "\u1eef": "u", "\u1eed": "u", "\u1ef1": "u", "\u1ee5": "u", "\u1e73": "u", "\u0173": "u", "\u1e77": "u", "\u1e75": "u", "\u0289": "u", "\u24e5": "v", "\uff56": "v", "\u1e7d": "v", "\u1e7f": "v", "\u028b": "v", "\ua75f": "v", "\u028c": "v", "\ua761": "vy", "\u24e6": "w", "\uff57": "w", "\u1e81": "w", "\u1e83": "w", "\u0175": "w", "\u1e87": "w", "\u1e85": "w", "\u1e98": "w", "\u1e89": "w", "\u2c73": "w", "\u24e7": "x", "\uff58": "x", "\u1e8b": "x", "\u1e8d": "x", "\u24e8": "y", "\uff59": "y", "\u1ef3": "y", "\u00fd": "y", "\u0177": "y", "\u1ef9": "y", "\u0233": "y", "\u1e8f": "y", "\u00ff": "y", "\u1ef7": "y", "\u1e99": "y", "\u1ef5": "y", "\u01b4": "y", "\u024f": "y", "\u1eff": "y", "\u24e9": "z", "\uff5a": "z", "\u017a": "z", "\u1e91": "z", "\u017c": "z", "\u017e": "z", "\u1e93": "z", "\u1e95": "z", "\u01b6": "z", "\u0225": "z", "\u0240": "z", "\u2c6c": "z", "\ua763": "z", "\uff10": "0", "\u2080": "0", "\u24ea": "0", "\u2070": "0", "\u00b9": "1", "\u2474": "1", "\u2081": "1", "\u2776": "1", "\u24f5": "1", "\u2488": "1", "\u2460": "1", "\uff11": "1", "\u00b2": "2", "\u2777": "2", "\u2475": "2", "\uff12": "2", "\u2082": "2", "\u24f6": "2", "\u2461": "2", "\u2489": "2", "\u00b3": "3", "\uff13": "3", "\u248a": "3", "\u2476": "3", "\u2083": "3", "\u2778": "3", "\u24f7": "3", "\u2462": "3", "\u24f8": "4", "\u2463": "4", "\u248b": "4", "\uff14": "4", "\u2074": "4", "\u2084": "4", "\u2779": "4", "\u2477": "4", "\u248c": "5", "\u2085": "5", "\u24f9": "5", "\u2478": "5", "\u277a": "5", "\u2464": "5", "\uff15": "5", "\u2075": "5", "\u2479": "6", "\u2076": "6", "\uff16": "6", "\u277b": "6", "\u2086": "6", "\u2465": "6", "\u24fa": "6", "\u248d": "6", "\uff17": "7", "\u2077": "7", "\u277c": "7", "\u24fb": "7", "\u248e": "7", "\u2087": "7", "\u247a": "7", "\u2466": "7", "\u2467": "8", "\u248f": "8", "\u24fc": "8", "\u247b": "8", "\u2078": "8", "\uff18": "8", "\u277d": "8", "\u2088": "8", "\u24fd": "9", "\uff19": "9", "\u2490": "9", "\u277e": "9", "\u247c": "9", "\u2089": "9", "\u2468": "9", "\u2079": "9" };     return function (str) {         if (typeof (str) == "undefined") return '';         var chars = str.split(''),             = chars.length - 1,             alter = false,             ch;         (; >= 0; i--) {             ch = chars[i];             if (diacritics.hasownproperty(ch)) {                 chars[i] = diacritics[ch];                 alter = true;             }         }         if (alter) {             str = chars.join('');         }         return str;     } })();  mynamespace.utilities.slugify = function (input) {     if (input == null) input = '';     return mynamespace.utilities.removediacritics(input)         .replace(/^\s\s*/, '') // trim start         .replace(/\s\s*$/, '') // trim end         .tolowercase() // camel case bad         .replace(/[^a-z0-9_\-\s]+/g, '') // exchange invalid chars         .replace(/[\-\s_]+/g, '-'); // swap whitespace single hyphen } 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -