10

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?

1
  • You can set the launch settings for debugging to point to your specific url. Just update your launchsettings.json file: "applicationUrl": "http://localhost:52608/auth/signin"However, that will only work for debugging. To truly have the default page direct you to the /auth/signin URL you need to use a redirect in the Home controller as @garret suggests. Commented Jan 12, 2018 at 17:03

2 Answers 2

9

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}/");
});
Sign up to request clarification or add additional context in comments.

4 Comments

My issue with this is I have other actions on my controller that I want to use attribute routing for. Can I really not set the startup url of the webapp?
You can try to use both routings in controller i will edit my anwser
@CBreeze or You can just add redirect action in web.config
Thanks for your answer though I don't belive there is a web.config file in ASP.Net Core?
1

There is a separate field for defaults.

routes.MapRoute(
      name: "default",
      template: "{controller}/{action}",
      defaults: new { controller = "auth", action = "signin" }
);

2 Comments

Where is the config you have used here?
its same as routes in your code ... I will change the code to reflect that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.