I am not sure if the below way is correct. It works for me but you should test it for your scenarios.
First create a user service to check username:
public interface IUserService
{
bool IsExists(string value);
}
public class UserService : IUserService
{
public bool IsExists(string value)
{
// your implementation
}
}
// register it
services.AddScoped<IUserService, UserService>();
Then create route constraint for username:
public class UserNameRouteConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
// check nulls
object value;
if (values.TryGetValue(routeKey, out value) && value != null)
{
var userService = httpContext.RequestServices.GetService<IUserService>();
return userService.IsExists(Convert.ToString(value));
}
return false;
}
}
// service configuration
services.Configure<RouteOptions>(options =>
options.ConstraintMap.Add("username", typeof(UserNameRouteConstraint)));
Finally write route and controllers:
app.UseMvc(routes =>
{
routes.MapRoute("default",
"{controller}/{action}/{id?}",
new { controller = "Home", action = "Index" },
new { controller = @"^(?!User).*$" }// exclude user controller
);
routes.MapRoute("user",
"{username:username}/{action=Index}",
new { controller = "User" },
new { controller = @"User" }// only work user controller
);
});
public class UserController : Controller
{
public IActionResult Index()
{
//
}
public IActionResult News()
{
//
}
}
public class NewsController : Controller
{
public IActionResult Index()
{
//
}
}
{username}. Does asp.net core handle that by looking at the identity? What happens ifhttp://mysite/johndoe/newsis a public facing url so anyone who's not authenticated is allowed to hit it?