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

Julian Dormon

We have built an MVC app that publishes a complete website with hierarchal Folders, SubFolders and Pages. The resulting pages, are strictly HTML and are not published in our MVC app. Our customers are able to name their Folders and Pages with any compliant string they choose. So conceivably, once the site is hosted, they could end up with a URL such as: someDomain.com/folder/subfolder1/subfolder2/page-slug. There is no limit to the number of nested subfolders.

We would like to replicate their sites in our MVC app, so that they are able to test them before they publish and perhaps so we can provide hosting ourselves if required.

The obvious problem, is how can we handle, ourMVCApp.com/folder/subfolder1/subfolder2/page-slug in an MVC app?

If there was a way that we could set routing to handle such a thing, then we could easily get the content required for the request by splitting the url into an array by "/".

The last segment would be a page contained in the previous segment's folder. We could then search our DB using these strings to get the required content.

Your help is greatly appreciated.

FURTHER QUESTION: In response to the answer provided by Tomi.

I added the code to my controller's class but I am receiving the following warning: enter image description here

I am not sure what I am missing? Did I put the code in the place? Thanks again.

UPDATE 2. I realized I had not actually created the controller factory, so I followed a partial example I found here: http://develoq.net/2010/custom-controller-factory-in-asp-net-mvc/. And since implementing it, I no longer receive any build-errors, but when I run the the debug, it crashes the built-in IISEXPRESS without any error message.

Here is my controller factory code:

public class FolderControllerFactory : IControllerFactory
{

    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        try
        {
            // Get the path
            string path = requestContext.RouteData.Values["pathInfo"].ToString();

            IController controller = new FolderController(path);

            return controller;
        }
        catch
        {
            // Log routing error here and move on

            return CreateController(requestContext, controllerName);

        }
    }


    public void ReleaseController(IController controller)
    {
        var disposable = controller as IDisposable;
        if (disposable != null)
        {
            disposable.Dispose();
        }
    }

    public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
    {
        return SessionStateBehavior.Default;
    }

}

Here is my global:

ControllerBuilder.Current.SetControllerFactory(typeof(ProofPixApp.Controllers.FolderControllerFactory));

And finally my controller:

public class FolderController : Controller
{
    private string _path;
    public FolderController(string path)
    {
        _path = path;
    }


    public ActionResult Index(string name)
    {
        ViewBag.Message = "Hello " + name;
        return View("/Views/" + _path);
    }
}

A couple of notes:

    1. I removed the 'override' from public IController CreateController
    because I kept receiving the initial error I posted.  

    2. I added public void ReleaseController and the public
       SessionStateBehavior GetControllerSessionBehavior methods to the
       CreateController class to avoid other build errors.

    3. I removed 'base.' from the catch clause because it too was causing a
       build error.

SOLUTION: I was able to avoid the error by checking to see pathValue was not null in the createController method, like so:

    public IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {

        // Get the path
        string path = "";
        if (requestContext.RouteData.Values["pathInfo"] != null)
        {
            path = requestContext.RouteData.Values["pathInfo"].ToString();
        }

        IController controller = new FolderController(path);

        return controller;

    }
Tomi Lammi

I have no idea what page slug is but here's my solution on how to achieve the routing you requested.

I made a custom ControllerFactory which handles the url and passes it to controller. This ControllerFactory constructs the controller we use to handle folder-route requests. We get the path from routevalues and then pass it to the FolderController.

    public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
    {
        try
        {
            // Get the path
            string path = requestContext.RouteData.Values["pathInfo"].ToString();

            IController controller = new FolderController(path);

            return controller;
        }
        catch
        {
            // Log routing error here and move on

            return base.CreateController(requestContext, controllerName);
        }
    }

Here's the controller. The actionmethod, which redirects to given path is called Index for now. The actionmethod returns view it finds from the url.

public class FolderController : Controller
{
    private string _path;
    public FolderController(string path)
    {
        _path = path;
    }
    public FolderController()
    {
    }

    public ActionResult Index(string name)
    {
        ViewBag.Message = "Hello " + name;
        return View("/Views/"+_path);
    }
}

Last step is to write our own route and register the factory. Open up RouteConfig.cs. My new RegisterRoutes method looks like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            name: "Dynamic",
            url: "{*pathInfo}",
            defaults: new { controller = "Folder", action = "Index", id = UrlParameter.Optional }
            );
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

In global.asax we need to register our FolderControllerFactory by adding this line into Application_Start method

ControllerBuilder.Current.SetControllerFactory(typeof(FolderControllerFactory)); 

And that's it! There's still much to be done, like handling improper urls and such. Also I don't think this supports plain html files, the files must be in .cshtml or asp format.

Here's the test:

My folder structure:

Views folder in studio

Url I request:

localhost:port/Mainfolder/Subfolder/Subfolder2/view.cshtml?name=Tomi

The result with Route Debugger plugin:

Web page

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

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

From Dev

Is it possible to load a partial view via string replacement in a controller in asp.net MVC 5

From Dev

How to Track Anonymous Users' Activities in ASP.Net MVC 5?

From Dev

Can the shared layout view have a controller in ASP.NET MVC?

From Dev

ASP.NET MVC Can I have a controller called PropertiesController?

From Dev

Can the shared layout view have a controller in ASP.NET MVC?

From Dev

Is it possible to have two models in one asp.net mvc view

From Dev

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

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

Asp.net MVC 5 not calling controller create method

From Dev

Controller for partial view in asp.NET 5 MVC

From Dev

Controller for partial view in asp.NET 5 MVC

From Dev

ASP.NET MVC5 RoutePrefix controller name

From Dev

ASP.NET MVC 5 dynamic controller routes

From Dev

if statement is not working inside controller mvc 5 asp.net

From Dev

Is it possible to compile a ASP.NET MVC 5 project with a mac

From Dev

Why is overloading in ASP.NET MVC5 not possible?

From Dev

How to allow anonymous visitor, but not allow logged in user on action method in ASP.NET MVC 5?

From Dev

Blocking anonymous access by default in ASP .NET 5

From Dev

Calling Classic ASP from ASP.NET MVC5 Controller on the same server

From Dev

Can i have more than one Post method in asp.net mvc controller?

From Dev

Is it possible to return two arrays at the view using one controller function at ASP.NET MVC4?

From Dev

Should my custom ASP.Net 5 MVC 6 Tag Helpers have an asp- prefix?

From Dev

Is it possible to have both Azure AD and Individual Account authentication in one ASP.NET MVC application?

From Dev

Is it possible to have a url parameter with a constant value through out the ASP.NET MVC application?

From Dev

ASP NET MVC 5 SessionState

Related Related

  1. 1

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

  2. 2

    Is it possible to load a partial view via string replacement in a controller in asp.net MVC 5

  3. 3

    How to Track Anonymous Users' Activities in ASP.Net MVC 5?

  4. 4

    Can the shared layout view have a controller in ASP.NET MVC?

  5. 5

    ASP.NET MVC Can I have a controller called PropertiesController?

  6. 6

    Can the shared layout view have a controller in ASP.NET MVC?

  7. 7

    Is it possible to have two models in one asp.net mvc view

  8. 8

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

  9. 9

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

  10. 10

    Inheritance from Controller in Asp.NET MVC 5

  11. 11

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

  12. 12

    Asp.Net MVC 5 has no Add New Controller?

  13. 13

    Asp.net MVC 5 not calling controller create method

  14. 14

    Controller for partial view in asp.NET 5 MVC

  15. 15

    Controller for partial view in asp.NET 5 MVC

  16. 16

    ASP.NET MVC5 RoutePrefix controller name

  17. 17

    ASP.NET MVC 5 dynamic controller routes

  18. 18

    if statement is not working inside controller mvc 5 asp.net

  19. 19

    Is it possible to compile a ASP.NET MVC 5 project with a mac

  20. 20

    Why is overloading in ASP.NET MVC5 not possible?

  21. 21

    How to allow anonymous visitor, but not allow logged in user on action method in ASP.NET MVC 5?

  22. 22

    Blocking anonymous access by default in ASP .NET 5

  23. 23

    Calling Classic ASP from ASP.NET MVC5 Controller on the same server

  24. 24

    Can i have more than one Post method in asp.net mvc controller?

  25. 25

    Is it possible to return two arrays at the view using one controller function at ASP.NET MVC4?

  26. 26

    Should my custom ASP.Net 5 MVC 6 Tag Helpers have an asp- prefix?

  27. 27

    Is it possible to have both Azure AD and Individual Account authentication in one ASP.NET MVC application?

  28. 28

    Is it possible to have a url parameter with a constant value through out the ASP.NET MVC application?

  29. 29

    ASP NET MVC 5 SessionState

HotTag

Archive