【问题标题】:MVC Routing with multiple parameters is not working具有多个参数的 MVC 路由不起作用
【发布时间】:2014-04-22 10:45:06
【问题描述】:

大家好,我添加了两条自定义路线

routes.MapRoute(
            "ParentCat",
            "{PCat}/{id}",
            new { Controller = "Adds", Action = "DetailWanted", PCat = UrlParameter.Optional, id = UrlParameter.Optional });

 routes.MapRoute(
            "SubCat",
            "{PCat}/{SCat}/{id}",
            new { Controller = "Adds", Action = "DetailWanted", PCat = UrlParameter.Optional, SCat = UrlParameter.Optional, id = UrlParameter.Optional });

网址

localhost:2110/Category/addid

&

localhost:2110/Category/SubCategory/addid

但调试器会直接移动并卡在自定义路由的 DetailWanted 操作中,甚至在初始化我的默认路由时卡住

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

没有被调用

【问题讨论】:

  • 多个可选参数可能是不可能的,但任何人都可以提供解决方案或替代方案

标签: asp.net-mvc-4 routing asp.net-mvc-routing


【解决方案1】:

我在

上遇到了一个解决方案

this thing solved my problem

然后将我的路线重写为

routes.MapRoute(
            name: "SubCat",
            url: "{PCat}/{SCat}/{id}",
            defaults: new { Controller = "Adds", Action = "Details" });//, id = UrlParameter.Optional PCat = UrlParameter.Optional, SCat = UrlParameter.Optional,

        routes.MapRoute(
            name: "ParentCat",
            url: "{PCat}/{id}",
            defaults: new { Controller = "Adds", Action = "Details" });//,PCat = UrlParameter.Optional,id = UrlParameter.Optional

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

控制器代码为

public ActionResult Details(string PCat = null, string SCat = null, int id = 0)
    {
        Add add = new Add();
        if (PCat == null && SCat == null && id > 0 && id != null)
        {
           add = db.Adds.Single(a => a.AddId == id);
        }
        if (SCat == null && PCat != null && id > 0 && id != null)
        {
           add = db.Adds.Single(a => a.AddId == id && a.Category.CategoryName == PCat);
        }
        if (SCat != null && PCat != null && id > 0 && id != null)
        {
            add = db.Adds.Single(a => a.AddId == id && a.Category.CategoryName == PCat && a.Category1.CategoryName == SCat);
        }
        if (add == null)
        {
            return HttpNotFound();
        }
        return View(add);
    }

而不是

public ActionResult DetailWanted(string PCat=null,string SCat=null, int id=0)
    {            
        if (PCat == "Adds" || PCat == null)
        {
            return RedirectToAction("Index", "Home");                
        }
        if (id > 0 && id != null)
        {
            if (SCat != null && PCat != null)
            {
                //return RedirectToAction("Details", "Adds" , new { @id = id });
               return Redirect("/Adds/Details/" + id);
            }
            else
            {
                return RedirectToAction("Details", "Adds" , new { @id = id });
            }
        }
        else
        {
           return RedirectToAction("Index");
        }

        return RedirectToAction("Index", "Home");}

【讨论】:

    猜你喜欢
    • 2016-10-01
    • 2019-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    相关资源
    最近更新 更多