0

Im trying to manage my routing settings for my projet but I don't understand everything, I want to make some actions or controllers names optional in the url.

For example I have a controller "HomeController" with a "SignIn()" action, I want it to be reachable with the url "/signin" and not "/home/signin".

Same for a controller named "ProjectsController" with an action "Details(int id)", I want it to be reachable with "/projects/348" and not "/projects/details/348".

I didn't modify the default endpoint configuration:

endpoints.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

How can I make some controllers name and actions name optional ? And after that will I be able to still use tag helpers for links with asp-controller and asp-action ?

1 Answer 1

1

I think you have to use route attributes. For example for your SignIn action

    public partial class HomeController : Controller
    {
        [Route("~/signin")]
        public IActionResult SignIn()
        {
            .....
        }
    }

and use the same template for another special actions

and you can have several routes for one action too

        [Route("~/signin")]
        [Route("~/home/signin")]
        public IActionResult SignIn()
        {
            .....
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Does the route attribute override the endpoint configuration in startup.cs ?
the route attributesdosn' t effect endpoints. The route attributes have the highest priority. Only if action doesn' t have routed attributes, than endpoints route is used.

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.