Copied to clipboard

Flag this post as spam?

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


  • Kap 21 posts 173 karma points
    Oct 27, 2024 @ 00:39
    Kap
    0

    Redirect from a ViewComponent

    Hi All,

    Not sure if this is possible, but I am trying to perform a redirection from within a View Component. From Google searches, I understand it would be better to do the redirect from the razor page, however in this case, it is not as straightforward.

    I can perform the redirect by injecting IHttpContextAccessor, in the View Component, and calling

    _httpContextAccessor.HttpContext.Response.Redirect(url);
    

    to redirect to the required page. However when I do this, Umbraco appears to process the View Component cshtml file anyway, and since a model is not being passed to the View, I am having to wrap all Razor code which accesses the Model in an if statement which checks the Model is not null.

    Is there a better way to do this?

    Thanks

    Kap.

  • Kap 21 posts 173 karma points
    Oct 27, 2024 @ 01:24
    Kap
    0

    I can use a flag in the model to indicate that we should abort the processing of the razor code

    eg:

    @if (Model.Page == null) { return; }
    

    Not sure if it is the most elegant solution, but appears to work.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies