Chlebta
Chlebta

Reputation: 3110

Route Issue ASP.net MVC 4 not redirecting to Index Action

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 }
            );
        }
}

Upvotes: 0

Views: 2105

Answers (1)

dotnetom
dotnetom

Reputation: 24901

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 }
    );
}

Upvotes: 2

Related Questions