Asp.net mvc ASP.NETMVC什么';我的路线是怎么走的?

Asp.net mvc ASP.NETMVC什么';我的路线是怎么走的?,asp.net-mvc,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc Routing,我有一个问题,我的url是丑陋的smth,如下所示: http://localhost:43547/Admin?id=1&str=wooh 我想看看像这样的网址 http://localhost:43547/LOL 我的路线是: routes.MapRoute( "MyRoute", "LOL", new { controller = "Admin", action = "Index", id = UrlParam

我有一个问题,我的url是丑陋的smth,如下所示:

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" }
       );
我有两个相同的观点,其中一个是用这种方法

@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();
    }
}
那怎么了?当我键入smth like/LOL时
它将我移动到另一个默认值为id和str的页面。

更改您的
MyRoute
定义以包含id和str参数值

routes.MapRoute(
       "MyRoute",
       "LOL",
       new { controller = "Admin", action = "Index", 
             id = 1, str = "wooh" }
       );

这样,你的url看起来像/LOL,但会传递你需要的值。

你想在这里做什么?只需输入/LOL,就可以得到默认值。你想让你的路线看起来像这样吗:
/LOL/1/wooh
?所以从你的角度来看,我无法隐藏默认值?我想我对你的默认值感到困惑。它们是id=5485和str=“Default Value”还是id=1和str=“wooh”?好的,看。。。默认值为id=5485和str=“默认值”。。。任务是在我将值从一个视图移动到另一个视图时隐藏这个丑陋的URL