سلام خسته نباشید ،
پروژه 2.1 رو به 3.1 اپدیت کردم ولی area که با endpoint هستد کار نمی کنند , صفحه های داخل area رو not found نشون میده
startUp
app.UseCookiePolicy();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
endpoints.MapAreaControllerRoute(name: "areas", "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
endpoints.MapControllers();
});
controller
[Area("Admin")]
[Route("Admin/[controller]/[action]")]
public class HomeController : Controller
{
[DisplayName("صفحه نخست")]
public async Task<IActionResult> Index()
{
return View();
}
}
سلام
فقط کافیه Enpoint تعریف کنید
فکر نکنم تنظیم دیگه ای داشته باشه
من دارم استفاده میکنم
1.الان مثلا بزنم localhost/admin سراغ area admin نمیره , و not found نشون میده و باید کامل بنویسم
2.الان در .net core 3 حتما برا کنترلر های area باید route تعریف کرد ؟
3.برا home درست شد ولی برای admin/account/login که [authorize] داره وقتی آدرسش رو میزنم این پیام و بر می گردونه
localhost redirected you too many times.
ERR_TOO_MANY_REDIRECTS
از روی مستندات مایکروسافت بررسی کنید
شاید چیزی از قلم افتاده
مشکل از قسمت service.addIdentity هست که در کور 3 adddefaultIdentity هست
الان این کدی که من داشتم برای .net core 2.2 بوده الان جواب میده بازم در .net core 3.1 و چه طور به 3.1 تبدیل کنم ؟
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.ConfigureApplicationCookie(options =>
{
// Cookie settings
options.LoginPath = "/Admin/Account/Login";
options.LogoutPath = "/Admin/Account/Login";
options.AccessDeniedPath = "/Account/AccessDenied";
options.ReturnUrlParameter = "returnTo";
options.SlidingExpiration = true;
options.ExpireTimeSpan = TimeSpan.FromHours(1);
});
ولی core 3.1
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
.AddEntityFrameworkStores<ApplicationDbContext>();
چه تفاوتی باهم دیگه دارند ؟
مستنداتش رو مطالعه کنید
کمی تفاوت دارند
خب الان حتما باید از AddDeaultIdentity استفاده کرد در .net core 3
الان این کدهای startup منه که به کنترلر های داراری [authorize] ارور ERR_TOO_MANY_REDIRECTS رو نشون میده ، مشکل از کجاست ؟
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"), c => c.MigrationsAssembly("DataLayer")));
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Password.RequiredLength = 6;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddTransient<DbContextSeedData>();
services.AddDistributedMemoryCache();
services.AddSession();
services.Configure<CookiePolicyOptions>(options =>
{
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
// Add application services.
services.AddTransient<ISMSSender, SMSSender>();
services.AddTransient<IEmailSender, EmailSender>();
services.AddTransient<IViewRender, ViewRenderServices>();
services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
services.AddControllersWithViews();
services.AddAuthorization(options =>
{
options.AddPolicy("Admin",
authBuilder =>
{
authBuilder.RequireRole("admin");
});
});
services.AddControllersWithViews(options =>
}
// 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.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
FileExtensionContentTypeProvider contentTypes = new FileExtensionContentTypeProvider();
contentTypes.Mappings[".apk"] = "application/vnd.android.package-archive";
app.UseHttpsRedirection();
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
// Cache static file for 1 year
if (!string.IsNullOrEmpty(context.Context.Request.Query["v"]))
{
context.Context.Response.Headers.Add("cache-control", new[] { "public,max-age=31536000" });
context.Context.Response.Headers.Add("Expires", new[] { DateTime.UtcNow.AddYears(1).ToString("R") }); // Format RFC1123
}
}
});
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseSession();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
//For Area
app.UseEndpoints(routes =>
{
routes.MapAreaControllerRoute(
name: "AdminPanel",
areaName: "Admin",
pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");
});
}