MVC路由与多个参数不工作 [英] MVC Routing with multiple parameters is not working

查看:116
本文介绍了MVC路由与多个参数不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有我添加了两个自定义路由

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

在网址

localhost:2110/Category/addid

&安培;

localhost:2110/Category/SubCategory/addid

但调试器直的动作和stucks在自定义路由的DetailWanted行动,甚至在我的init默认路由

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

不叫

推荐答案

我碰到一个解决方案上

<一个href=\"http://stackoverflow.com/questions/14169796/route-with-two-optional-parameters-in-mvc3-not-working\">this事情解决了我的问题

然后重写我的路线为

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

与控制器code作为

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

而不是

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

这篇关于MVC路由与多个参数不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆