Davide
Davide

Reputation: 1345

MVC 4 URL rewrite

I don't find how I can create a "fake" subfolder in MVC4.

I want that an URL like

AREA/CONTROLLERNAME/FAKEFOLDER/ACTION/

goes to

AREA/CONTROLLERNAME/ACTION

Is it possibile? Any suggestions? Thanks!

Upvotes: 0

Views: 2202

Answers (2)

Ulises
Ulises

Reputation: 13429

You could add a MapRoute that expects an extra parameter in your route (fakefolder in the example below). Then, the routing occurs as usual, where the controller's action serves the page. Example:

routes.MapRoute(
          name: "FakeFolder",
          url: "{controller}/{fakefolder}/{action}",
          defaults: new { controller = "home", action = "index", fakefolder = UrlParameter.Optional}
      );

Notice that with this routing you can use any "folder name" since it is just a placeholder.

Url example: myController/SomeFakeFolder/someAction will execute the action someAction in myController

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1039498

Have you tried using routing? For example assuming you have an Admin area:

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

Now when you request /admin/home/fakefolder/index the Index action of HomeController within the Admin are will be executed.

Upvotes: 1

Related Questions