locked
MVC 5 Routeconfig Routing Url.Action returns null RRS feed

  • Question

  • User-1838394278 posted

    I spend 3 hours and didnt get success. I cant figure out the right solution.

    This is what I am trying and getting null. My homepage is working but the others is not working.

    Url.Action("ShowDilKursuMenu","DilKursu") => getting null
    
    

    I am trying getting like this from this code url action https://localhost:44313/dil-kurslari for my parent html menu. Html child menu have like https://localhost:44313/dil-kurslari/germany or https://localhost:44313/dil-kurslari/germany/frankfurt

     #region DilKursu
            routes.MapRoute(
                name: "DilKursuUlkeEyaletSehir",
                url: "dil-kurslari/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}",
                defaults: new
                {
                    controller = "DilKursu",
                    action = "ShowDilKursuMenu",
                    ulke = UrlParameter.Optional,
                    eyalet = UrlParameter.Optional,
                    sehir = UrlParameter.Optional,
                    ilce = UrlParameter.Optional,
                    firma = UrlParameter.Optional,
                });
    
            routes.MapRoute(
                name: "DilKursuAll",
                url: "dil-kursu/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}/{kursAdi}",
                defaults: new
                {
                    controller = "DilKursu",
                    action = "DilKursuDetay",
                    ulke = UrlParameter.Optional,
                    eyalet = UrlParameter.Optional,
                    sehir = UrlParameter.Optional,
                    ilce = UrlParameter.Optional,
                    firma = UrlParameter.Optional,
                    kursAdi = UrlParameter.Optional,
                });
    
            #endregion
            #region HomeIndex2
            routes.MapRoute(
                name: "HomeLang2",
                url: "",
                defaults: new { controller = "Home", action = "HomeIndex" }
            );
            #endregion

    One important thing is DilKursuAll named route required all of this parameters.

    Thursday, October 3, 2019 9:25 PM

Answers

  • User-17257777 posted

    Hi alicankablan55,

    When it has multiple parameters in url, only the last one can be null.

    url: "dil-kursu/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}/{kursAdi}",

    So you can add a routing fragment in your RouteConfig.

    routes.MapRoute(
                    name: "DilKursuUlkeEyaletSehir1",
                    url: "dil-kursu",
                    defaults: new
                    {
                        controller = "DilKursu",
                        action = "ShowDilKursuMenu"
                    });
    

    Test Result:

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 11:00 AM

All replies

  • User-17257777 posted

    Hi alicankablan55,

    When it has multiple parameters in url, only the last one can be null.

    url: "dil-kursu/{ulke}/{eyalet}/{sehir}/{ilce}/{firma}/{kursAdi}",

    So you can add a routing fragment in your RouteConfig.

    routes.MapRoute(
                    name: "DilKursuUlkeEyaletSehir1",
                    url: "dil-kursu",
                    defaults: new
                    {
                        controller = "DilKursu",
                        action = "ShowDilKursuMenu"
                    });
    

    Test Result:

    Best Regards,

    Jiadong Meng

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 7, 2019 11:00 AM
  • User-1838394278 posted

    Thank you very much for the information :)

    Friday, October 18, 2019 10:50 AM