ASP.NET MVC 5: single controller method to handle paths in file browser mode

Mr. Pumpkin

I want to have some controller with the single method that would allow me to navigate through some hierarchy (file system etc.).

In other words I want to have possibility to access this method with flexible routes and get part of routes as parameter. For example in case of this hierarchy

Root
  Sub-folder-A
  Sub-folder-B
    Sub-folder-C

I want to have access folders with the next routes

mymvcapplication/explorer/root
mymvcapplication/explorer/root/sub-folder-a
mymvcapplication/explorer/root/sub-folder-b/sub-folder-c

What and where should I configure to implement it properly?

Shyju

To support variable number of url parameter values in the request url, you can mark your method parameter with * prefix in the route definition.

With MVC Attribute routing,

[Route("explorer/root/{*levels}")]
public ActionResult Details(string levels = "")
{
    if (String.IsNullOrEmpty(levels))
    {
        //request for root
    }
    else
    {
        var levelArray = levels.Split('/');
        //check level array and decide what to do 
    }
    return Content("Make sure to return something valid :) ");
}

The last parameter prefixed with * is like a catch-all parameter which will store anything in the url after explorer/root

So when you request yoursite.com/explorer/root/a/b/c/d , the default model binder will map the value "a/b/c/d" to the levels parameter. You can call the Split method on that string to get an array of url segments.

To enable attribute routing, go to RouteConfig.cs and call the MapMvcAttributeRoutes() method in the RegisterRoutes.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapMvcAttributeRoutes();

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Asp.net MVC 5 not calling controller create method

From Dev

return a View and File in a single method in ASP.Net MVC

From Dev

Handle exception in asp.net mvc controller

From Dev

Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller

From Dev

ASP.NET MVC 5 calling controller method from Javascript Error

From Dev

How do I handle HTML5 Multiple File uploads in ASP.NET MVC?

From Dev

API controller can't be found in browser in mvc asp.net

From Dev

API controller can't be found in browser in mvc asp.net

From Dev

sending image file to controller action method in asp.net mvc4

From Dev

Mutliple authorizations in a single browser in ASP.NET MVC

From Dev

ASP.NET MVC - JSON.NET extension method for controller

From Dev

Link to a file in MVC 5 ASP.NET

From Dev

ASP.NET MVC 5 Handle Unauthorize Request

From Dev

Asp.Net 5 MVC 6 detect mobile browser

From Dev

How to upload video in ASP.NET MVC 5 and pass the video file from a view to a method?

From Dev

Asp.net MVC Ajax call not calling controller method

From Dev

ASP.NET MVC RemoteAttribute does not trigger action method in controller

From Dev

ASP.NET MVC: How to override parent Controller method

From Dev

Asp.net MVC Ajax call not calling controller method

From Dev

How to pass value of particular row to controller method in asp .net mvc

From Dev

how can use a custom method in controller in asp net mvc

From Dev

ASP.NET MVC Basics - Controller method does not return string

From Dev

ASP.NET 5 / MVC 6 Ajax post Model to Controller

From Dev

Is it possible to have an anonymous controller in asp.net MVC 5?

From Dev

How to disable default route on controller ASP.NET MVC 5

From Dev

Inheritance from Controller in Asp.NET MVC 5

From Dev

ASP.NET MVC5: Unit Testing a controller with a session

From Dev

Asp.Net MVC 5 has no Add New Controller?

From Dev

Controller for partial view in asp.NET 5 MVC

Related Related

  1. 1

    Asp.net MVC 5 not calling controller create method

  2. 2

    return a View and File in a single method in ASP.Net MVC

  3. 3

    Handle exception in asp.net mvc controller

  4. 4

    Sending data by using AngularJS to an action method of ASP.NET MVC 5 controller

  5. 5

    ASP.NET MVC 5 calling controller method from Javascript Error

  6. 6

    How do I handle HTML5 Multiple File uploads in ASP.NET MVC?

  7. 7

    API controller can't be found in browser in mvc asp.net

  8. 8

    API controller can't be found in browser in mvc asp.net

  9. 9

    sending image file to controller action method in asp.net mvc4

  10. 10

    Mutliple authorizations in a single browser in ASP.NET MVC

  11. 11

    ASP.NET MVC - JSON.NET extension method for controller

  12. 12

    Link to a file in MVC 5 ASP.NET

  13. 13

    ASP.NET MVC 5 Handle Unauthorize Request

  14. 14

    Asp.Net 5 MVC 6 detect mobile browser

  15. 15

    How to upload video in ASP.NET MVC 5 and pass the video file from a view to a method?

  16. 16

    Asp.net MVC Ajax call not calling controller method

  17. 17

    ASP.NET MVC RemoteAttribute does not trigger action method in controller

  18. 18

    ASP.NET MVC: How to override parent Controller method

  19. 19

    Asp.net MVC Ajax call not calling controller method

  20. 20

    How to pass value of particular row to controller method in asp .net mvc

  21. 21

    how can use a custom method in controller in asp net mvc

  22. 22

    ASP.NET MVC Basics - Controller method does not return string

  23. 23

    ASP.NET 5 / MVC 6 Ajax post Model to Controller

  24. 24

    Is it possible to have an anonymous controller in asp.net MVC 5?

  25. 25

    How to disable default route on controller ASP.NET MVC 5

  26. 26

    Inheritance from Controller in Asp.NET MVC 5

  27. 27

    ASP.NET MVC5: Unit Testing a controller with a session

  28. 28

    Asp.Net MVC 5 has no Add New Controller?

  29. 29

    Controller for partial view in asp.NET 5 MVC

HotTag

Archive