MVC Routing with multiple parameters is not working

試著忘記壹切 提交于 2019-12-25 03:05:19

问题


Hey All I Added two custom routes

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

for the urls

localhost:2110/Category/addid

&

localhost:2110/Category/SubCategory/addid

but debugger straight moves and stucks in the custom route's DetailWanted action and even on init my default route

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

is not called


回答1:


i came across a solution on

this thing solved my problem

and then rewrote my routes as

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

with controller code as

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

instead of

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


来源:https://stackoverflow.com/questions/23217271/mvc-routing-with-multiple-parameters-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!