0

I don't completely understand how to use routing in asp.net mvc. I've organized my views and controllers in 2 main folders: frontoffice and backoffice. Now I'd like to have the actions inside backoffice that build the url in this way: frontoffice/controller/action/parameters And the ones inside the folder frontoffice in the classical way: controller/action/parameter

I wrote these rules but they aren't correct

routes.MapRoute(
    name: "Backoffice",
    url: "Backoffice/{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

routes.MapRoute(
    name: "Frontoffice",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

In this way all the action inside frontoffice builds url with 'backoffice'. How should I do it?

Mino
  • 635
  • 10
  • 28
  • Which version of MVC? Did you try adding two `Area`? – Tomas Jansson Aug 30 '12 at 12:48
  • Is the slash at the beginning of the FrontOffice route a typo or is it there on purpose? have you tried removing that from the route definition? – JTMon Aug 30 '12 at 12:53
  • You should look into what [Areas](http://www.codeguru.com/csharp/.net/net_asp/mvc/article.php/c20227/Using-Areas-in-ASPNET-MVC-Application.htm) are in MVC. When those are created, it will updated your routing engine accordingly. – Mark Oreta Aug 30 '12 at 13:43

1 Answers1

0

Folders don't matter in ASP.NET MVC.

It doesn't matter what folder you put something in, that doesn't correspond to where the routing engine is going to look for it.

Here's how you do it:

The convention is to put your controllers in a folder named Controller, but this is just convention I don't think it affects routing (though it might).

Make sure your controller has the name Controller appended to it, and inherits either from the Base Controller class, or IController. For instance:

public class MyController : Controller

Second, your routing is for the URL.

routes.MapRoute(
    "Backoffice",
    "backoffice/{controller}/{action}/{id}"

To get to that route, you need two things, neither of which have to do with your directory structure:

  • Have a controller that derives from Controller or implements IController
  • call that controller appropriately: /backoffice/home/list/1

If you want your controllers to be based on folder structure, then you may want to use the namespace argument, and make sure your namespaces match up with the directory structure.

You'd change your routing to look like this:

routes.MapRoute(
    "backoffice",
    "backoffice/{controller}/{action}/{id}",
    new[] { "MyProject.Backoffice.Controllers" }
);

Then your directory structure should be:

MyProject 
  |
  - FrontOffice

  - BackOffice
    | 
    - Controllers
      |
      - MyController.cs

And the namespace for your controller should be:

namespace MyProject.Backoffice.Controllers
{
    public class MyController
George Stocker
  • 57,289
  • 29
  • 176
  • 237