locked
Generic Action Route RRS feed

  • Question

  • User-195907812 posted

    I need the page name to be passed to my controller:

    URL examples:

    www.mydomain.com/site/mypage1
    www.mydomain.com/site/mypage2
    www.mydomain.com/site/mypage3

    I've added the route as follows:

    routes.MapRoute(
         name: "route_site",
         url: "site/{action}",
         defaults: new { controller = "siteController", action = "Index" }
    );

    and I've added the following to the site controller:

    public ActionResult Index(string action)
    {
         string test = action;

         return View();
    }

    But all I'm getting is "The resource cannot be found" - can anyone see what I'm doing wrong?

    Wednesday, December 12, 2018 3:59 PM

All replies

  • User475983607 posted

    I need the page name to be passed to my controller:

    Can you explain why you are using a single controller, forcing the URL to start with site, and removed the URL parameter?  What problem are you trying to solve?

    and I've added the following to the site controller:

    public ActionResult Index(string action)
    {
         string test = action;

         return View();
    }

    The action is "Index" not "string action".  The parameter in your snippet is "string action".

    /site/Index?action=someValue

    See the MVC routing docs.

    https://docs.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

    Wednesday, December 12, 2018 4:14 PM
  • User-195907812 posted

    Can you explain why you are using a single controller, forcing the URL to start with site, and removed the URL parameter?  What problem are you trying to solve?


    I'd like to use the pagename (slug) to retrieve some data from the database.

    I'm still confused (most of my approach is taken from other threads). I thought that if route would default to Index.

    Wednesday, December 12, 2018 4:18 PM
  • User475983607 posted

    A slug is a URL to a resource.  What does a slug have to do with your route configuration?  What exactly are you trying to do?

    I'd like to use the pagename (slug) to retrieve some data from the database.

    But that's already wired up in MVC.  The Controller.Action is invoked using the URL Controller/Action.

    I'm still confused (most of my approach is taken from other threads). I thought that if route would default to Index.

    It does default to "Index".  But your route configuration is very limiting.  

    Wednesday, December 12, 2018 4:30 PM
  • User-195907812 posted

    In this scenario, where the URLs are as follows:

    www.mydomain.com/site/mypage1
    www.mydomain.com/site/mypage2
    www.mydomain.com/site/mypage3

    I use 'controller' (site) and the 'action' (mypage1 etc) as parameters and then access my database table to retrieve the page content. 

    Edit: I think one of the things that's maybe confusing me from those documents is that I don't want "Index" in my URL.

    Wednesday, December 12, 2018 4:33 PM
  • User475983607 posted

    In this scenario, where the URLs are as follows:

    www.mydomain.com/site/mypage1
    www.mydomain.com/site/mypage2
    www.mydomain.com/site/mypage3

    I use 'controller' (site) and the 'action' (mypage1 etc) as parameters and then access my database table to retrieve the page content. 

    Edit: I think one of the things that's maybe confusing me from those documents is that I don't want "Index" in my URL.

    I think this what you're looking for...

    https://haacked.com/archive/2010/02/21/manipulating-action-method-parameters.aspx/

    Where the slug is a route parameter that resolves to an Id.

    Wednesday, December 12, 2018 5:10 PM
  • User1520731567 posted

    Hi RageRiot,

    RageRiot

    routes.MapRoute(
         name: "route_site",
         url: "site/{action}",
         defaults: new { controller = "siteController", action = "Index" }
    );

    Try to code like the below in RouteConfig.cs:

    1. put default MapRoute behind your new MapRoute
    2. if you have Multiple namespaces,don't forget add constraint
    3. controller = 'Site' not 'siteController'
    4. if mypage1,mypage2,mypage3 actions do not have parameters,you could navigatate:  xxxx/site/mypage1, xxxx/site/mypage2, xxxx/site/mypage3.
    5. if have parameters,you could navigatate like: xxxx/site/mypage1?para=1
    namespace TestApplication1
    {
        public class RouteConfig
        {
            public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");          
    
                routes.MapRoute(
                     name: "route_site",
                     url: "site/{action}/{id}",
                     defaults: new {controller = "Site", action = "Index", id = UrlParameter.Optional },
                     namespaces: new string[] { "TestApplication1.Controllers" }//if you have Multiple namespaces
                );
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "About", id = UrlParameter.Optional },
                    namespaces: new string[] { "TestApplication1.Controllers" }//if you have Multiple namespaces
                );
            }
        }
    }
    

    RageRiot

    public ActionResult Index(string action)
    {
         string test = action;

         return View();
    }

    But all I'm getting is "The resource cannot be found" - can anyone see what I'm doing wrong?

    Now,using the above MapRoute,you could jump this Index action,but I suggest you better change the parameter name, ‘string action’ and MapRoute have the same name, it will causing confusion.

    You could try and check the code.

    Best Regards.

    Yuki Tao

    Thursday, December 13, 2018 10:03 AM
  • User-474980206 posted

    {action} is reserved for the controller action name. try: {page}

    routes.MapRoute(
         name: "route_site",
         url: "site/{page}",
         defaults: new { controller = "siteController", action = "Index" }
    );

    public ActionResult Index(string page)
    {
         string test = action;
         return View();
    }

    also this route must go before the default route.

    Thursday, December 13, 2018 3:08 PM