Copied to clipboard

Flag this post as spam?

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


  • Kap 19 posts 169 karma points
    29 days ago
    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 19 posts 169 karma points
    29 days ago
    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.

Please Sign in or register to post replies

Write your reply to:

Draft