Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Index as the default route for a controller

I have a url

  • http://www.roadkillwiki.org/Page/Index/documentation

which I want to turn into

  • http://www.roadkillwiki.org/Page/documentation

That could also be something like http://www.roadkillwiki.org/Page/my-url-with-spaces - the parameter is a string. The route setup I've tried is:

routes.MapRoute(
    "ControllerDefault",
    "{controller}/{id}",
    new { controller = "Page", action = "Index", id = UrlParameter.Optional }
);

However this is interfering with the default "id" route that MVC projects come with. Is there any way of achieving this?

like image 600
Chris S Avatar asked Mar 09 '11 22:03

Chris S


People also ask

What is the default action for a controller?

By default, Controller is the Home Controller and default Action Method is the “Index” action method.

What is the default setting for route config in MVC?

Routing in ASP.NET MVC By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional). Now let us create one MVC Application and open RouteConfig.

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

How do I add a controller to my route?

Basic Controllers You can define a route to this controller method like so: use App\Http\Controllers\UserController; Route::get('/user/{id}', [UserController::class, 'show']);


2 Answers

You don't need to lose the default route. The key to avoiding your routes interfere with each other is to order them so the more specific rules precede the less specific ones. For example:

// Your specialized route
routes.MapRoute(
    "Page",
    "Page/{slug}",
    new { controller = "Page", action = "Index" }
);

// Default MVC route (fallback)
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Then your PageController would look like this:

using System.Web.Mvc;

public class PageController : Controller
{
    public string Index(string slug)
    {
        // find page by slug
    }
}

That said, I would strongly advice you to do this instead:

// Your specialized route
routes.MapRoute(
    "Page",
    "Page/{id}/{slug}",
    new { controller = "Page", action = "Index", slug = UrlParameter.Optional }
);

// MVC's default route (fallback)
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

And your PageController:

using System.Web.Mvc;

public class PageController : Controller
{
    public string Index(int id)
    {
        // find page by ID
    }
}

By including the page ID either at the beginning of your URL (like StackOverflow does) or at the end, you can then just ignore the slug, and instead retrieve your pages by ID. This will save you a ton of headaches if your users change the page name. I have gone through this and it's painful; you basically have to keep a record of all names your pages have had in the past, just so your visitors/search engines don't get a 404 every time a page is renamed.

Hope this helps.

like image 55
Daniel Liuzzi Avatar answered Sep 19 '22 23:09

Daniel Liuzzi


If you don't need a default route that came with project template you can set up one like this:

routes.MapRoute(
    "ControllerDefault",
    "{controller}/{pagename}",
    new { controller = "Page", action = "Index" }
);

And than in your controller you would have an action:

        public ActionResult Index(string pagename)
        {
            //do something
        }
like image 37
frennky Avatar answered Sep 19 '22 23:09

frennky