简体   繁体   中英

Trying to understand MVC routing

We have several controllers we would like grouped in a subfolder (Admin) on our site. We would have the main pages be at the root level. But for these pages we would like to have our site path be something like this:

www.domain.com/Admin/{controller}/{action}/{id}

I've set the RouteConfig.cs file like this:

routes.MapRoute
(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Submission", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute
(
    name: "Admin",
    url: "Admin/{controller}/{action}/{id}",
    defaults: new { controller = "SystemSecurity", action = "Index", id = UrlParameter.Optional }
);

I've set up one of the controllers like this:

[RoutePrefix("Admin/SystemSecurity")]
public class SystemSecurityController : Controller
{
    private MkpContext _db = new MkpContext();

    // GET: SystemSecurity
    public ActionResult Index()
    {
        var roles = _db.Role.Select(r => r);

        return View(roles.ToList());
    }
}

In our solution the path to the controller is: \\Controllers\\Admin\\SystemSecurityController.cs

The path to the view is: \\Views\\Admin\\SystemSecurity\\Index.cshtml

But we get the 'Resource cannot be found' error message.

I've also tried it with no RoutePrefix, and also with RoutePrefix("Admin").

If I put the view here: \\Views\\SystemSecurity\\Index.cshtml
and navigate with this path: www.domain.com/SystemSecurity/Index
the page loads, so I know the controller and page are working.

What am I doing wrong?

I found out about MVC Areas. ( http://www.philliphaydon.com/2011/07/mvc-areas-routes-order-of-routes-matter/ )

By adding an Area to my project (Right click on Project's name, then Add - Area) I am able to better group my code.

Many of the pages I found either don't mention Areas or when the do, it's in passing, but they don't explain them. Hopefully this will help somebody else.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM