簡體   English   中英

路由問題ASP.net MVC 4沒有重定向到索引操作

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

當我嘗試使用以下URL格式訪問控制器上的索引頁面時:

mydomain/taux/index

這有效,並且Index頁面打開。 但是,如果我使用這種url格式(我不將Index操作添加到URL)

 mydomain/taux/

我收到“ 404未找到”,但通常它應該可以正常工作,並自動將我重定向到索引頁。

請如何解決?

Taux控制器:

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

這是因為您的默認操作設置為“登錄”。 如果要將鏈接mydomain / taux /重定向到Indext操作,則需要將其設置為Index。

如果只想為此特定控制器進行重定向,則可以使用以下路由定義:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM