0

我当前的网站要求用户在访问子菜单之前选择他们的餐厅类型。

例如,当用户尝试直接访问时,http://localhost:8888/Restaurant/KFCMenu/?id=KFCCurlyFries 他们将被重定向到此页面http://localhost:8888/Restaurant/Home

这里我想提一下的逻辑是因为他们想要访问肯德基卷饼菜单(在参数中),路线应该根据参数自动分配餐厅类型并跳过Home controller/Index选择。

在这种情况下,我可以知道如何编写我的自定义路由来绕过 Home 控制器/索引吗?

这是我在 RouteConfig.cs 中的默认路由

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

更新:

我的餐厅控制器

 public class RestaurantController: Controller
    {
        public ActionResult Index(HomeModel vm, string btnModeMcd, string btnModeKFC, string btnModePizzaHut, string Menu)
        {
            if (!string.IsNullOrWhiteSpace(Menu))
            {
                TempData["Success"] = "<script>Swal({" +
                            "title: 'Access Denied'," +
                            "text: 'Access Denied for " + Menu + " Menu'," +
                            "type: 'warning'," +
                            "timer: 2500," +
                            "confirmButtonColor: '#5cb85c'," +
                            "cancelButtonColor: '#3085d6'," +
                            "confirmButtonText: 'OK'," +
                            "cancelButtonText: 'New Menu'" +
                        "});</script>";
            }

            if (string.IsNullOrWhiteSpace((string)Session["MODE"]))
            {
                Session["MODE"] = "Mcd";
            } 

            if (btnModeMcd != null)
            {
                Session["MODE"] = "Mcd";
            }

            if (btnModeKFC != null)
            {
                Session["MODE"] = "KFC";
            }

            if (btnModePizzaHut != null)
            {
                Session["MODE"] = "PizzaHut";
            }
            
            vm.Mode = (string)Session["MODE"];
            return View(vm);
        }
        
        public ActionResult About()
        {
            ViewBag.Message = "Your description page.";

            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult AccessDenied(string Menu)
        {
            TempData["Success"] = "<script>Swal({" +
                        "title: 'Access Denied'," +
                        "text: 'Access Denied for " + Menu + " Menu'," +
                        "type: 'warning'," +
                        "timer: 2500," +
                        "confirmButtonColor: '#5cb85c'," +
                        "cancelButtonColor: '#3085d6'," +
                        "confirmButtonText: 'OK'," +
                        "cancelButtonText: 'New Menu'" +
                    "});</script>";
            return View();
        }
    }
4

1 回答 1

0

你有很多事情要做,但你没有表现出足够的实际答案

//url: Restaurant/Index?menu="someValue"
public class RestaurantController: Controller
{
    //you need to be clear if this is a post or get..
    [HttGeet]
    public ActionResult Index(HomeModel vm, string menu)
    {
        //do check if menu is valid, return 404 if not
        Session["MODE"] = menu; //why not call this RestaurantName
        return View();
    }
}
        
//what is HomeModel
于 2020-10-09T06:25:41.360 回答