Bonjour à tous,

Je ne comprend pas bien comment fonctionne les RouteUrl.

Voici mon code, si jamais il manque des choses, j'éditerai mon post pour l'ajouter directement dedans.

Ce que je cherche à faire c'est de générer des URLs de cette forme : LocalHost:12345/Mode/Femmes/Robes/Robe-Moulante/
Le problème c'est que quelque sois la façon dont je génère l'url dans mon fichier de vue en razor l'url sois ressemble à ça LocalHost:12345/ListOrGrid/ListOrGrid/4?UrlFomer=Mode/Femmes/Robes/RobeMoulante/ ou bien le href de mon <a> est juste vide à la génération Oo

Le contenu de ce que j'envoie pour les ID provient d'une table qui à cette forme :

id id_Parent Nom_Categorie
1 Mode
2 1 Femmes
3 2 Robes
4 3 RobeMoulante


ListOrGridController.cs

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
public ActionResult ListOrGrid(short categorieID = 1)
        {
            List<ArticlePrevisualisation> articlePrevisualisation = new List<ArticlePrevisualisation>();
 
            //foreach (var item in collection)
            //{
            //    ArticlePrevisualisation artPrev = new ArticlePrevisualisation();
            //    ARTICLES_DEFINITIONSet aRTICLES_DEFINITIONSet = new ARTICLES_DEFINITIONSet();
            //    aRTICLES_DEFINITIONSet = db.ARTICLES_DEFINITIONSet.Where(w => w.);
            //    ARTICLESSet aRTICLESSet = new ARTICLESSet();
            //    articlePrevisualisation.Add(artPrev);
            //}
 
 
            return View();
        }
RouteConfig.cs

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
 
public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
 
 
            routes.MapRoute(
                name: "Index",
                url: "",
                defaults: new { controller = "Home", action = "Index" });
 
 
            routes.MapRoute(
                name: "Categorie",
                url: "{urlFormer}",
                defaults: new { controller = "ListOrGrid", action = "ListOrGrid", id = UrlParameter.Optional, urlFormer = UrlParameter.Optional }
            );
 
 
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
GlobalFunction.cs

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
 
 
    /// <summary>
    /// forme une url propre sans caractère spéciaux ni accent
    /// </summary>
    /// <param name="EntryUrl"></param>
    /// <returns></returns>
    public static string FormatUrl(string EntryUrl)
    {
        string ExitUrl = EntryUrl;
        ExitUrl = ExitUrl.Replace(" ", "");
        ExitUrl = ExitUrl.Replace("-", "");
        ExitUrl = ExitUrl.Replace("%", "");
        ExitUrl = ExitUrl.Replace("/", "");
        ExitUrl = ExitUrl.Replace("&", "");
        ExitUrl = ExitUrl.Replace("'", "");
        ExitUrl = ExitUrl.Replace("__", "");
        ExitUrl = ExitUrl.Replace("_", "");
        ExitUrl = ExitUrl.Replace("é", "e");
        ExitUrl = ExitUrl.Replace("à", "a");
        ExitUrl = ExitUrl.Replace("ù", "u");
        ExitUrl = ExitUrl.Replace("è", "e");
        ExitUrl = ExitUrl.Replace("ä", "a");
        ExitUrl = ExitUrl.Replace("â", "a");
        ExitUrl = ExitUrl.Replace("ê", "e");
        ExitUrl = ExitUrl.Replace("ë", "e");
        ExitUrl = ExitUrl.Replace("ï", "i");
        ExitUrl = ExitUrl.ToLower();
        return ExitUrl;
    }
 
    /// <summary>
    /// Récupère la liste des noms des catégories parentes pour former l'url
    /// </summary>
    /// <param name="categorieID"></param>
    /// <returns>String formaté avec les noms des catégories récupérées</returns>
    public static string nomCategories(short categorieID)
    {
        string url = "";
        try
        {
            List<short> listIdCategorieURL = new List<short>();
            do
            {
                short catId = new short();
                catId = db.CATEGORIESSet.Where(w => w.Categorie_Id == categorieID).Select(s => s.Categorie_Id).FirstOrDefault();
                int i = 0;
                listIdCategorieURL.Add(catId);
                categorieID = new short();
                categorieID = db.CATEGORIE_PARENTSSet.Where(w => w.Categorie_Id == categorieID).Select(s => s.Categorie_ParentId).FirstOrDefault();
            } while (categorieID != 0);
 
 
            listIdCategorieURL.Reverse();
 
 
            foreach (short item in listIdCategorieURL)
            {
                if(listIdCategorieURL.Last() == item)
                    url += nomCategorie(item);
                else
                    url += nomCategorie(item) + "/";
            }
 
 
            return url;
        }
        catch(ArgumentNullException ex)
        {
            Debug.WriteLine(ex);
            return "";
        }
        catch(InvalidOperationException ex)
        {
            Debug.WriteLine(ex);
            throw ex;
        }
        catch(NullReferenceException ex)
        {
            Debug.WriteLine(ex);
            return "";
        }
        catch(EntityException ex)
        {
            db.Dispose();
            Debug.WriteLine(ex);
            return "";
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
            throw ex;
        }
    }
_Layout.cshtml

Code : Sélectionner tout - Visualiser dans une fenêtre à part
<a href="@Url.RouteUrl("Categorie", new { id = cat.Categorie_Id, urlFormer = GlobalFunction.FormatUrl(GlobalFunction.nomCategories(cat.Categorie_Id)) })" class="level-top"><span>@cat.Categorie_Intitule</span> </a>
Je pense avoir tout donné !