Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ASP.NET MVC中绕过主控制器/索引的自定义路由_C#_Asp.net_Asp.net Mvc_Routeconfig - Fatal编程技术网

C# ASP.NET MVC中绕过主控制器/索引的自定义路由

C# ASP.NET MVC中绕过主控制器/索引的自定义路由,c#,asp.net,asp.net-mvc,routeconfig,C#,Asp.net,Asp.net Mvc,Routeconfig,我当前的网站要求用户在访问子菜单之前选择他们的餐厅类型 例如,当用户尝试直接访问http://localhost:8888/Restaurant/KFCMenu/?id=KFCCurlyFries 他们将被重定向到此页面http://localhost:8888/Restaurant/Home 这里我要提到的逻辑是,由于他们想要访问肯德基卷曲薯条菜单(在参数中),路线应根据参数自动分配餐厅类型,并跳过要选择的主控制器/索引 我可以知道在这种情况下如何编写自定义路由以绕过主控制器/索引吗 这是我在

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

例如,当用户尝试直接访问
http://localhost:8888/Restaurant/KFCMenu/?id=KFCCurlyFries
他们将被重定向到此页面
http://localhost:8888/Restaurant/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();
        }
    }
公共类餐馆控制器:控制器
{
公共操作结果索引(HomeModel虚拟机、字符串btnModeMcd、字符串btnModeKFC、字符串btnModePizzaHut、字符串菜单)
{
如果(!string.IsNullOrWhiteSpace(菜单))
{
TempData[“Success”]=“Swal({”+
标题:“访问被拒绝”+
文本:“+菜单+”菜单的访问被拒绝”+
键入:“警告”+
计时器:2500+
“confirmButtonColor:'#5cb85c'”+
“cancelButtonColor:'#3085d6'”+
“confirmButtonText:‘确定’,”+
“cancelButtonText:'新菜单'+
"});";
}
if(string.IsNullOrWhiteSpace((string)会话[“MODE”]))
{
会话[“模式”]=“Mcd”;
} 
if(btnModeMcd!=null)
{
会话[“模式”]=“Mcd”;
}
if(btnModeKFC!=null)
{
会话[“模式”]=“肯德基”;
}
if(btnModePizzaHut!=null)
{
会话[“模式”]=“比萨屋”;
}
vm.Mode=(字符串)会话[“Mode”];
返回视图(vm);
}
关于()的公共行动结果
{
ViewBag.Message=“您的描述页。”;
返回视图();
}
公共行动结果联系人()
{
ViewBag.Message=“您的联系人页面。”;
返回视图();
}
公共操作结果访问被拒绝(字符串菜单)
{
TempData[“Success”]=“Swal({”+
标题:“访问被拒绝”+
文本:“+菜单+”菜单的访问被拒绝”+
键入:“警告”+
计时器:2500+
“confirmButtonColor:'#5cb85c'”+
“cancelButtonColor:'#3085d6'”+
“confirmButtonText:‘确定’,”+
“cancelButtonText:'新菜单'+
"});";
返回视图();
}
}

你有很多事情要做,但你没有给出足够的答案

//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

路由定义为可用的,而不是流执行。。。。“所以我会说你把注意力放在了错误的事情上。”我明白了。关于我应该用什么方式来实现我的目标有什么建议吗?你能分享一下餐厅管理员的相关代码吗?老实说,很难遵循,因为你对需要发生的事情的描述很难遵循,这可能就是为什么你在为自己的处境而挣扎。建议可以更好地构建您的q,以便人们更好地理解。用户位于第x页,并且。。。。。他们被重定向了为什么?那么他们不是真的在x上,他们现在在y上,然后。。。这很让人困惑,因为他们访问这些页面的唯一方式是你重定向它们。。。那你为什么感到困惑呢。最好用更好的描述来展示更多的代码。我想我现在就来。。。你想阅读url来假设位置…,那么你需要为此编写代码。阅读url并基于此创建逻辑。要么传递价值观,要么将你满意的价值观存储在你满意的地方,不管是赞成还是反对。会话、cookie、本地存储。你的另一页是如何设置的。。。。也许用同样的方法。我又在猜你想做什么。。。。但这些都是你需要编码的东西。