views:

42

answers:

3

My current Routing rules are

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

I have several controllers that can only be accessed by the Admin. Currently I have a menu at:

/Admin/Index

Which lists a bunch of action links. Right now clicking any of those links redirects like this example:

/News/Index

But I need it to be like:

/Admin/News/Index

This current setup sort of works, but links on my homepage are being caught by the wrong rule and are going to, for example /Admin/Article/1 when they should be just /Article/1

I've searched StackOverflow for the answer, and found some that come close - but I still don't understand routing well enough to make use of their answers. Any assistance is appreciated.

EDIT

Here are a collection of links from the homepage that are being caught by the routing rules incorrectly

<div id="menucontainer">

                <ul id="menu">              
                    <li><%: Html.ActionLink(" ", "About", "Home", null, new { ID = "AboutMHN" })%></li>
                    <li><%: Html.ActionLink(" ", "Products", "Home", null,  new { ID = "Products" })%></li>
                    <li><%: Html.ActionLink(" ", "HubLocator", "Home", null, new { ID = "HubLocator" })%></li>
                    <li><%: Html.ActionLink(" ", "ResellerProgram", "Home", null, new { ID = "ResellerProgram" })%></li>
                    <li><%: Html.ActionLink(" ", "ResellerLogin", "Home",  null, new { ID = "ResellerLogin" })%></li>
                    <li><%: Html.ActionLink(" ", "ContactUs", "Home", null, new { ID = "ContactUs" })%></li>
                </ul>                 

            </div>

Also, my Admin controller just has an index action. I have controllers for all of the other 'Admin' pages, for example my NewsController has actions for index, create, edit, delete, for listing all the news articles, and performing crud operations. Am I structuring this incorrectly?

My AdminController.cs

 [Authorize(Roles = "Administrator")]
    public class AdminController : Controller
    {
        //
        // GET: /Admin/

        public ActionResult Index()
        {
            return View();
        }


    }

The Admin/Index this returns contains a menu with ActionLinks just like

<li><%: Html.ActionLink("News Articles", "Index", "News") %></li>

My NewsController.cs has Actions for performing CRUD operations. I would like the url to be

Admin/News/Edit/5

Instead of

News/Edit/5
A: 

Flip the two:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

If that doesn't work, add the code for the News menu links to your question.

Martin
Yeah I guess the Admin rule is being too 'Greedy'? Its grabbing all the links on my homepage this way.
Gallen
A: 

The mapping is from top to bottom, the first route that can apply will be used. In your case you have Admin route under Default. Since Default can be used it will, therefore you should place Admin route above Default. The problem with that is that all will use that route. You need to be a bit more specific, perhaps you should remove {controller} so that only Admin actions will use that route. Need more info to better advise.

Sayed Ibrahim Hashimi
I updated my question with some more information on what I am attempting to do.
Gallen
A: 

I needed to use Areas.

See: http://elegantcode.com/2010/03/13/asp-net-mvc-2-areas/

I created a new Area called Admin, renamed my AdminController.cs to MenuController.cs and placed all of the required controllers and views inside of the Admin area folder. Inside the Admin area folder there is a file called AdminAreaRegistration.cs which contains

public class AdminAreaRegistration : AreaRegistration
{
    public override string AreaName
    {
        get
        {
            return "Admin";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_default",
            "Admin/{controller}/{action}/{id}",
            new { controller= "Menu", action = "Index", id = UrlParameter.Optional }
        );
    }
}

I had to add in the controller = "Menu" so that when you navigate to /Admin it will take you to /Admin/Menu/Index by default. Now any of my controllers in the area folder are accessible like /Admin/News/Edit/5 and so forth. Thanks for your attempts, I should have been more clear that I am an absolute MVC noob and didn't know about Areas to begin with.

Gallen
The way I understand areas, I don't think you are doing it right. I think your ActionLinks are incorrect. You should be using route links and specifying a route name.
Martin