我有一个问题,我的网址是丑陋的样子:
http://localhost:43547/Admin?id=1&str=wooh
我希望看到像这样的网址
http://localhost:43547/LOL
我的路线是:
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = UrlParameter.Optional, str = UrlParameter.Optional }
);
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index" }
);
我有2个相同的视图,其中一个是这样的方法
@Html.ActionLink("Click me", "Index", "Admin", new { id = 1, str = "wooh" }, null)
移动页面......
我有两个控制器:
public ActionResult Index(int id = 5485, string str = "Default Value")
{
ViewBag.ID = id;
ViewBag.STR = str;
return View();
}
public class HomeController : Controller
{
public ActionResult Index()
{
ViewBag.Controller = "Home";
ViewBag.Action = "Index";
return View();
}
}
那有什么不对?当我输入类似/ LOL的smth时 它将我移动到另一个页面,其默认值为id和str ...
答案 0 :(得分:0)
更改MyRoute
定义以包含id和str参数值。
routes.MapRoute(
"MyRoute",
"LOL",
new { controller = "Admin", action = "Index",
id = 1, str = "wooh" }
);
这样你的网址看起来像/ LOL,但会传递你需要的值。