.NET Core compression does not work in my controller - asp.net-core

I just created a new .NET Core 3.1 Web API project based by the template in VS 2019, and I added code following the official example in startup.cs like this:
public void ConfigureServices(IServiceCollection services)
{
services.AddResponseCompression();
services.AddControllers();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
but the compression middleware does not work
webapi response in browser

.Net Core Response Compression is disabled by default for HTTPS traffic, which it appears you are using. Try changing your call to AddResponseCompression() to turn it on for https.
services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
You can see the MS docs for use of response compression with https, as there are security implications noted in the article. https://learn.microsoft.com/en-us/aspnet/core/performance/response-compression?view=aspnetcore-3.1#compression-with-secure-protocol

Related

starup file changes in Asp.net core

I am Upgrading .Net core web api solution 2.2 to 3.1
I have question what should i use in statrup.cs file for 3.1
// currently i am using this
public Startup(Microsoft.AspNetCore.Hosting.IHostingEnvironment env)
public Startup(Microsoft.Extensions.Hosting.IHostingEnvironment env)
I am Upgrading .Net core web api solution 2.2 to 3.1 I have question what should i use in statrup.cs file for 3.1
If you create a new ASP.NET Core 3.1 API project using the API template, you would find it uses below Startup class.
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
Compare it with old one, you can find the following changes:
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2) is changed to services.AddControllers()
IHostingEnvironment is obsolete, and it uses IWebHostEnvironment now
app.UseEndpoints, endpoint routing is used

Is this usage of CORS correct?

While in VS2019 Debugging (IIS Express) an API service and web application using the service I receive "Access to XMLHttpRequest at 'theservice' from origin 'apporigin' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource".
Startup Methods
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddCors();
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
app.UseCors(options => {
options.AllowAnyOrigin();
});
}
Controller Class and Get Method both have the [EnableCors()] attribute.
To me, this seems correct however I continue to receive the missing origin header message. Am I implementing CORS correctly in this case?
UseCors must be called in proper order. It must be placed after UseRouting, but before UseAuthorization. You can refer this document.
Not every middleware needs to go in this exact order, but many do. For example:
UseCors, UseAuthentication, and UseAuthorization must go in the order shown.
UseCors currently must go before UseResponseCaching due to this bug.
https://stackoverflow.com/users/6527049/vivek-nuna has the answer in Comments.
move app.UseCors(options => {options.AllowAnyOrigin();}); to after app.UseRouting() and before app.UseAuthorization()
The correct Configure Method looks like this:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(options => {
options.AllowAnyOrigin();
});
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}

My razor pages are not working all of the generate the same error : Localhost page cant be found

This is my startup.cs file
Does anyone know whats going wrong This is my index.cshtml
Alright so my code will be below its very basic startup.cs file and a index.cshtml file with some basic code just for testing
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}}
#page
#DateTime.Now
Try to create a new project and check whether you have the same problem.
And did you apply something to your service side? You can share it.
Or you can try these code first:
ConfigureServices:
services.AddMvc(options =>
{
options.EnableEndpointRouting = false;
});
Configure:
app.UseMvcWithDefaultRoute();

ASP.NET Core 3.0 in endless redirection loop

I have recently upgraded an existing ASP.NET Core 2.2 web app to 3.0. Everything compiles. We use Azure ADB2C for authentication. Previously the user would be directed to the login page and upon entering their credentials they would be navigated to the Default page.
After upgrading to ASP.NET Core 3.0 however, after they have been directed to the login page the app goes into an infinite re-direction loop.
I can login correctly using an older version of the app (that still uses ASP.NET Core 2.2) so it's not the Azure ADB2C configuration. It must be some mis-configuration with the routing in the ASP.NET Core 3.0 app.
Here is my startup configuration.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc(option => option.EnableEndpointRouting = false);
services.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))
//ensure the user must be authenticated before they can access any of the pages from the app
services.AddMvc()
.AddRazorPagesOptions(options => { options.Conventions.AuthorizeFolder("/"); });
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseMvcWithDefaultRoute();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseAuthentication();
}
Most of this code has remained unchanged and was working correctly when running under ASP.NET Core 2.2. Since I have upgraded to ASP.NET Core 3.0 it goes into an infinite re-direction loop.
I can't see what could be causing the problem.
The order of your middleware is incorrect.app.UseMvcWithDefaultRoute(); is required to be placed after app.UseAuthentication();
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
}

How to set default razor page route with asp.net 3.0 middleware

So to setup endpoint routing in asp.net core 3.x, we do something like this
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
//...
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
How/where can we define a "default" page route other than index?
The easiest solution would be to manually add route to the custom page in ConfigureServices
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages(o => o.Conventions.AddPageRoute("/CustomPage", ""));
}
With this solution you need to rename or remove Index page to avoid AmbiguousMatchException
This is an example of a default route.
app.UseEndpoints(endpoint =>
{
endpoint.MapDefaultControllerRoute();
});