Reputation: 817
I'm converting an Asp.Net Core 2.2 Mvc application to 3.0; when I try to refactor the code to use UseEndpoints instead of UseMvc, the routes don't appear to be recognized.
The following is the working code from startup.cs using UseMvc
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/EventLog/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseRequestLocalization();
app.UseAuthentication();
app.UseMvc(endpoints =>
{
endpoints.MapRoute("Grid", "Grid/{action}/{view}",
new
{
Controller = "Grid",
Action = "View"
});
endpoints.MapRoute("default", "{controller=Portal}/{action=Index}/{id?}");
});
}
And this is the refactored code for UseEndpoints
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/EventLog/Error");
app.UseHsts();
}
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseRequestLocalization();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("Grid", "Grid/{action}/{virtualAction}",
new
{
Controller = "Grid",
Action = "View"
});
endpoints.MapControllerRoute("default", "{controller=Portal}/{action=Index}/{id?}");
});
}
When I run the application with the UseEndpoints code, I get an error from my custom AuthorizationHandler because there are no values in httpContextAccessor.HttpContext.Request.RouteValues. If I remove the MapControllerRoute code for the Grid route, the default routes work fine with the AuthorizationHandler. Can you have multiple controller route patterns when using UseEndpoints?
Upvotes: 0
Views: 3727
Reputation: 20126
I tried your code in below sample and it works well to get value of IHttpContextAccessor
:
1.My custom authorization handler
public class MinimumAgeRequirement : IAuthorizationRequirement
{
public int MinimumAge { get; }
public MinimumAgeRequirement(int minimumAge)
{
MinimumAge = minimumAge;
}
}
public class MinimumAgeHandler : AuthorizationHandler<MinimumAgeRequirement>
{
IHttpContextAccessor _httpContextAccessor = null;
public MinimumAgeHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
MinimumAgeRequirement requirement)
{
var data = _httpContextAccessor.HttpContext.Request.RouteValues;
//...
}
}
2.ConfigureServices:
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthorization(options =>
{
options.AddPolicy("AtLeast21", policy =>
policy.Requirements.Add(new MinimumAgeRequirement(21)));
});
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton<IAuthorizationHandler, MinimumAgeHandler>();
}
3.Controller
[Authorize(Policy = "AtLeast21")]
public class GridController : Controller
Upvotes: 1