Asp.net core https redirect ERR_TOO_MANY_REDIRECTS (on IIS)

Hi,

Your project after you deploy on IIS; when you want to do http to https redirect;
“ERR_TOO_MANY_REDIRECTS” brush up against.

For Solution add in startup.cs;

public void ConfigureServices(IServiceCollection services)
{
  services.Configure<ForwardedHeadersOptions>(options =>
            {
                options.ForwardedHeaders = ForwardedHeaders.XForwardedProto;
            });
  services.AddHttpsRedirection(opts => {
                opts.RedirectStatusCode = StatusCodes.Status301MovedPermanently;
                opts.HttpsPort = 443;
            });
}

and,

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
..
 app.UseHttpsRedirection();
..
}

Asp.Net Core Deploying ERR_TOO_MANY_REDIRECTS ERROR ON IIS

Hi everyone,

You made a asp.net core. Everyting is okey in local. But on IIS deploying there is a problem.

Solution:

 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />

to

 <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />

Asp.Net Core and Cookies

Enable Cookies in Project for:

Startup.cs file in insert

And this using cookies for:
 string username = Request.Cookies["username"];
 string password = Request.Cookies["password"];

 

Asp.Net Core Session Not Working (Solution)

Startup.cs file in;

services.Configure<CookiePolicyOptions>(options =>
            {              
                options.CheckConsentNeeded = context => true;
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

change to:
options.CheckConsentNeeded = context => false;

Kestrel Server Different Port with Run For Asp.Net Core (Nginx)

Create kestrel service:

nano /etc/systemd/system/kestrel-oguzhanabali.com.service
[Unit]
Description=Oguzhan ABALI Blog

[Service]
WorkingDirectory=/var/www/oguzhanabali.com
ExecStart=/usr/bin/dotnet /var/www/oguzhanabali.com/OguzhanAbali.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target

Add the code below:

Environment=ASPNETCORE_URLS=http://localhost:5001

Asp.Net Core appsettings json get value in static class

Example appsettings:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

Create MyConfig name class and;

using Microsoft.Extensions.Configuration;
static class ConfigurationManager
{
    public static IConfiguration AppSetting { get; }
    static ConfigurationManager()
    {
        AppSetting = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
    }
}

Now you can use it like :

string value = ConfigurationManager.AppSetting["value_Key"];

Asp.net MVC Css Menu Active Class

Selamlar,

Solutionunuzda “Helpers” adında bir folder oluşturduktan sonra HtmlHepers.cs adında bir class oluşturuyorsunuz ve bu class aşağıdaki kodlardan oluşuyor.

public static class HtmlHelpers

{

public static string ActivePage(this HtmlHelper helper, string controller, string action)

{

string classValue = "";

string currentController = helper.ViewContext.Controller.ValueProvider.GetValue("controller").RawValue.ToString();

string currentAction = helper.ViewContext.Controller.ValueProvider.GetValue("action").RawValue.ToString();

if (currentController == controller && currentAction == action)

{

classValue = "active";

}

return classValue;

}

}

 

Sonrasında _layout türevi menüyü yazdığınız yerde class=”@Html.ActivePage(“Home”,”Index”)” ekliyorsunuz. Bu size active stringini döndürecek. classValue=”active” bunu eğer active classınız farklıysa değiştirebilirsiniz.

İyi çalışmalar herkese..

No response while calling API in ASP.NET C# Web Forms

Asp.net webform uygulamasında async bir api call işlemi yapmaya çalışıyorsanız webformun async çalışmasını doğrulamalısınız.

Async=True
Orn: <%@ Page Title=”” Language=”C#” Async=”true” MasterPageFile=”~/Mast.. gibi

Daha sonra HttpClient ile post yaptığınız yerde ConfigureAwait(false) demelisiniz.

Orn: HttpResponseMessage response = await client.PostAsJsonAsync(“api/user”, userDTO).ConfigureAwait(false); gibi.

Solution:

HttpResponseMessage response = await client.PostAsJsonAsync("api/user", userDTO).ConfigureAwait(false);

 

Bir daha ki ipucunda görüşmek üzere, İyi çalışmalar…