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?
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.
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?
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:
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
is working on a reply...