Copied to clipboard

Flag this post as spam?

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


  • Peter 24 posts 102 karma points
    Mar 02, 2017 @ 11:53
    Peter
    0

    Changing the default controller - Is this correct?

    Hi

    I'm working at a site - there some of the functionality is placed in the Masterpage (view).

    Some of this functionality could issue a Response.Redirect - which caused that the Antiforgerytoken further down the page (in a partial view) would cause an error at the server: Server cannot append header after HTTP headers have been sent.

    To avoid this - I tried to move the functionality to a CustomRenderController inheriting from Umbraco.Web.Mvc.RenderMvcController and added the following to the ApplicationStarting event:

    DefaultRenderMvcControllerResolver.Current.SetDefaultControllerType(typeof(CustomRenderController));
    

    I can now issue a Return Redirect(“/”) from the CustomRenderController. But can I do that/should I do that? It is not hitting the return base.Index(model) then – is this a problem?

    If I can/should – a new problem arises. I need to return an error message to the front page, which I’m redirecting to. I have tried using TempData – but it gets emptied before I can read it from the frontpage view. What is the best way to transfer this error message?

  • David Peck 690 posts 1896 karma points c-trib
    Mar 07, 2017 @ 13:12
    David Peck
    0

    Your use of the default controller should be just fine, and sounds like a sensible approach. Returning a redirect is totally appropriate and there is no need to call base.Index().

    As for you passing data to your view after a redirect, TempData is the way to go. If it isn't working then make sure you're not reading that value twice as it will get removed after the 1st attempt. It also uses the Session to store that data so you'll need to make sure the Session is setup correctly.

  • Peter 24 posts 102 karma points
    Mar 08, 2017 @ 06:50
    Peter
    1

    Thanks for the feedback – I could use some more inputs – so I will try to describe the situation more detailed and include an extra question.

    When a visitor visits a page, he can either be logged in or not in our system. We do not use Umbraco’s Membership for handling/storing a user – but our own implementation. We connect to another external system – where the user can be logged either in or out. If the user is logged out from this system (for some reason) – we also need to log him out from our system.

    The way it is done today is – that in the masterpage – there is a check against our session storage - if the visitor is logged in – if he is – we check if he is logged out from the external system. If that is the case (for some reason) – we log him out of our system to – and redirects him to the front page with an error message.

    At the same time –we might need the same information from the session storage in a partial view – so the same call as mentioned above for getting information about being logged in (only our system) is called again.

    What I would like to do is:

    Move the check if the user is logged in at our system (and the external) system to the custom default controller. I do not belive it belongs in the view. This is what my initial post was about. As I understand the implementation I have suggested is correct?

    What would be nice is – if I somehow could be able to access the session information at all views (and partial views) – so we do not need to call it several times in the view. Is it somehow possible to return an object (extend an already existing object) – so this information is included? I hope it is – if so how can I achieve this.

Please Sign in or register to post replies

Write your reply to:

Draft