HELP! How do you REDIRECT to an external site outside a Razor Component using HttpContext.Response.Redirect

Arthur Ochoa 1 Reputation point
2021-07-15T16:01:20.423+00:00

GIVEN: This is a Blazor Server Project

WHAT WORKS: HttpContext works when you inject IHttpContextAccessor inside the Razor component. Inside my Index.razor file, I have the following:

@page "/"
@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor

<p>Welcome to your new app.</p>

@code {

    protected override void OnInitialized()
    {
        httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");
    }
}

THE ISSUE: Access to HttpContext outside a Razor component returns a null value (i.e. in a separate Class file).

WHAT I HAVE DONE SO FAR:

I added on Startup.cs

        services.AddHttpContextAccessor();
        services.AddScoped<HttpContextAccessor>();

I created a separate class (MyBase.cs):

using System;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace BlazorApp.Server.Pages
{
    public class MyBase : ComponentBase
    {
        private IHttpContextAccessor _httpContextAccessor;
        private IServiceProvider _services;

        public MyBase(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }
       public void AuthenticateSite()
       {
            _httpContextAccessor = _services.GetRequiredService<IHttpContextAccessor>();
            _httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");
       }
    }
}

Then, I added the following in my Index.razor file:

@page "/"
@inherit MyBase
@inject IHttpContextAccessor httpContextAccessor

@code
{
    protected override void OnInitialized()
    {
        MyBase myBase = new MyBase(httpContextAccessor);
        myBase.AuthenticateSite();
    }
}

THE GOAL: I would like to have a separate class to handle the RESPONSE.REDIRECT.

I'm new to Blazor development, anyone with some idea or technical expertise who can help me with the issue is greatly appreciated. Thanks in advance.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,403 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Zhi Lv - MSFT 32,021 Reputation points Microsoft Vendor
    2021-07-16T07:51:35.7+00:00

    Hi @Arthur Ochoa ,

    THE ISSUE: Access to HttpContext outside a Razor component returns a null value (i.e. in a separate Class file).

    Please set a break point in the AuthenticateSite() method, then debug your code, the issue relates the IServiceProvider, the _services is null. So, it will throw the ArgumentNullException: Value cannot be null. (Parameter 'provider' error.

    Try to comment or remove _services relate code, the redirect action should work.

        public void AuthenticateSite()  
        {   
              //_httpContextAccessor = _services.GetRequiredService<IHttpContextAccessor>();  
             _httpContextAccessor.HttpContext.Response.Redirect("https://www.site1.com/auth" + "?return_url=" + "https://www.mysite.com/blazorapp");  
        }  
    

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Dillion