Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default route not working in Asp.Net Core

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?

like image 780
CBreeze Avatar asked Dec 15 '17 11:12

CBreeze


People also ask

How would you setup the default route using endpoints?

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 .

What is default route in asp net?

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.

How do I use MapControllerRoute?

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.


2 Answers

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}/");
});
like image 50
garret Avatar answered Sep 20 '22 00:09

garret


There is a separate field for defaults.

routes.MapRoute(
      name: "default",
      template: "{controller}/{action}",
      defaults: new { controller = "auth", action = "signin" }
);
like image 26
cpr43 Avatar answered Sep 18 '22 00:09

cpr43