I have the following Controller
& Action
that I want to be the first page that is loaded when the user enters my webapp:
[Route("auth")]
public class AuthController : Controller
{
[Route("signin")]
public IActionResult SignIn(bool signInError)
{
}
}
I try to do this in my app.UseMvc
options like so:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=auth}/{action=signin}/");
});
However, when I start my webapp it simply navigates to http://localhost:52608/
.
If I type in http://localhost:52608/auth/signin?ReturnUrl=%2F
the webpage loads correctly on the page I want to be on when the user starts the webapp.
My question is, how do I set this up so it can navigate to this page as soon as the user opens the webapp?
UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .
The default route table contains a single route (named Default). The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id. The Default route maps this URL to the following parameters: controller = Home.
MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.
Mixed Routing It is perfectly valid to use convention-based routing for some controllers and actions and attribute routing for others. However, ASP.NET Core MVC does not allow for convention-based routes and attribute routing to exist on the same action. If an action uses attribute routing, no convention-based routes can map to that action. See the ASP.NET Core docs for more info.
More info about routing
So if you want to use attribute routing, then You can't map default path in convention based routing.
If you need to use attribute routing in this controller you can add redirect action in web.congig file. Or just remove attribute routing from that action and it will work:
public class AuthController : Controller
{
public IActionResult SignIn(bool signInError)
{
}
}
Edit:
Easiest solution: Add new controller with redirect action for example:
public class HomeController : Controller
{
public IActionResult Index()
{
return new RedirectToActionResult("SignIn", "Auth", new {signInError = false});
}
}
And add default routing to this Controller
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=home}/{action=index}/");
});
There is a separate field for defaults.
routes.MapRoute(
name: "default",
template: "{controller}/{action}",
defaults: new { controller = "auth", action = "signin" }
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With