0

When I try to access to index Page on controller if I use this URL format :

mydomain/taux/index

This works and the Index page is opened. But if I use this url format (I don't add the Index action to URL)

 mydomain/taux/

I got "404 not found", but normally it should works and redirect me to index page automatically.

How to fix it please ?

Taux Controller :

// GET: /Taux/
public ActionResult Index()
{
    var taux = db.TAUX.Include(t => t.CATEGORIE).Include(t => t.GARANTIE);
    return View(taux.ToList());
}

RouteConfig.cs:

public class RouteConfig
{
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );
        }
}

1 Answer 1

2

This is because your default action is set to Login. You need to set it to Index if you want link mydomain/taux/ to redirect to Indext action

If you want to have the redirect just for this specific controller you can use the following route definitions:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Taux",
        url: "Taux/{action}/{id}",
        defaults: new { controller = "Taux", action = "Index", id = UrlParameter.Optional }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
    );
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yeah this work's but I want to change my Startup page from Home/Index to Account/login Only the sartup page.
See updated answer on how you can configure custom route for single controller

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.