Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ben Haynie 20 posts 50 karma points
    Dec 04, 2013 @ 21:19
    Ben Haynie
    0

    Ability to redirect to error page from MVC Code behind

    I have not been able to find a similar thread to this, but I am looking to develop a few MVC Templates that will need to have the ability to re-direct to an error page if the user does not have rights based on a parameter passed in through the URL.

    In Umbraco, you can set up a page to have restricted access. You can specify the roles that are able to access that page. I need to go one step further, however, and also limit access based on a parameter that will be passed via the URL. I will need to write some custom code that will retrieve the parameter from the URL, check our internal system to see if the user has appropriate access, and if not re-direct the user to an error page.

    I know that I can write most of this into Surface Controller and a Partial View, and simply render an error message on the Partial View if the user doesn't have rights. However, I would prefer if there were a way to validate rights in the MVC equivalent of a page load, and if the user doesn't have access automatically redirect to the same error page that is configured through the Umbraco Public Access Role Based Protection window (and not have to hard code in my controller).

    Is this possible? How would I go about doing this?

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Dec 05, 2013 @ 12:09
    Jeroen Breuer
    0

    Have a look at the Hybrid Framework: http://our.umbraco.org/projects/developer-tools/hybrid-framework

    Everything will always go through a controller (route hijacked or default controller). All controllers inherit from a BaseSurfaceController and which has the following code:

    protected override void OnException(ExceptionContext filterContext)
    {
        if (filterContext.ExceptionHandled)
        {
            return;
        }
    
        //Log the exception.
        Umbraco.LogException(filterContext.Exception);
    
        //Clear the cache if an error occurs.
        var cacheManager = new OutputCacheManager();
        cacheManager.RemoveItems();
    
        //Show the view error.
        filterContext.Result = View("Error");
        filterContext.ExceptionHandled = true;
    }
    
    #endregion

    So if an error happens it will redirect to a view called error. I don't pass in a model so the error page just contains some html.

    Jeroen

Please Sign in or register to post replies

Write your reply to:

Draft