[英]Custom Route in ASP.NET MVC to bypass Home Controller/Index
我當前的網站要求用戶在訪問子菜單之前選擇他們的餐廳類型。
例如,當用戶嘗試直接訪問http://localhost:8888/Restaurant/KFCMenu/?id=KFCCurlyFries
時,他們將被重定向到此頁面http://localhost:8888/Restaurant/Home
我想在這里提到的邏輯是因為他們想要訪問 KFC Curly Fries 菜單(在參數中),路由應該根據參數自動分配餐廳類型並跳過Home controller/Index
來選擇。
我可以知道在這種情況下如何編寫自定義路由來繞過 Home 控制器/索引嗎?
這是我在 RouteConfig.cs 中的默認路由
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "UserLogin", action = "Index", id = UrlParameter.Optional }
);
更新:
我的餐廳 controller
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();
}
}
你有很多事情要做,但你沒有表現出足夠的實際答案
//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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.