ASP.net 核心 3.1:Javascript:window.open:Razor 页面

ASP.net core 3.1: Javascript: window.open: Razor page

我正在尝试在我的 javascript 中打开剃刀页面方法,但我不确定为什么它在 http 上打开,而不是在 https 上打开,即使 url 设置为 https。

下面是我的代码:

Javascript:

function openWindowWithPost(url, data) {
  var form = document.createElement("form");
  form.target = "_blank";
  form.method = "POST";
  form.action = url;

  form.style.display = "none";

  for (var key in data) {
    var input = document.createElement("input");
    input.type = "hidden";
    input.name = key;
    input.value = data[key];
    form.appendChild(input);
  }
  var input = document.createElement("input");
  var token = $('input:hidden[name="__RequestVerificationToken"]').val();
  input.name = "__RequestVerificationToken";
  input.type = "hidden";
  input.value = token;
  form.appendChild(input);

  document.body.appendChild(form);
  form.submit();
  document.body.removeChild(form);
}

function ButtonClickedEvent(){
    var inputJSON = '{"id": 1}';
    var url = location.protocol + '//' + location.host + '/Report/?handler=BatchReport';
    console.log('url: ' + url);    
    openWindowWithPost(url, JSON.stringify(inputJSON));        

}

Razor 页面:隐藏代码:

[Authorize]
[HttpPost]
public async Task<string> OnPostBatchReport([FromBody] InputParameter parametersJSON)
{
    var somestring = await getstring(parametersJSON);
    return somestring ;

}

Startup.cs:

public void ConfigureServices(IServiceCollection services)
{       

    services.AddRazorPages().AddJsonOptions(options => options.JsonSerializerOptions.PropertyNamingPolicy = null);
    services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");


    services.Configure<CookiePolicyOptions>(options =>
    {
        // This lambda determines whether user consent for non-essential cookies is needed for a given request.
        options.CheckConsentNeeded = context => true;
        options.MinimumSameSitePolicy = SameSiteMode.Unspecified;                
        options.Secure = CookieSecurePolicy.SameAsRequest;                
        options.HandleSameSiteCookieCompatibility();
    });

    // Sign-in users with the Microsoft identity platform
    services.AddMicrosoftIdentityWebAppAuthentication(Configuration);

    services.AddControllersWithViews(options =>
    {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        options.Filters.Add(new AuthorizeFilter(policy));
    }).AddMicrosoftIdentityUI();

    
}

我的网站托管在 https://localhost:12345,当我点击按钮时,我调用 ButtonClickedEvent javascript 函数在单独的选项卡中打开报告页面。此选项卡的协议为 http,而非 https。

感谢任何帮助。

关于应用hostURL通常取决于launchsettings.json文件配置。

To resolve this issue you should set applicationUrl under launchsettings.json as what you want it to while your application run.

launchsettings.json:

希望它能为您提供相应的指导。