asp.net mvc路由清除地址栏

asp.net mvc routing clean address bar

使用剃须刀时 url.action 来电,

<a href="@Url.Action("Index", "Profile", new { ThemeCode = 'SOC' })">

然后我在地址栏中得到这个link

xxx.com/Profile?ThemeCode=SOC

我想将属性发送到配置文件控制器中的索引方法,但我也希望地址栏中的外观更干净。

像这样:

xxx.com/soc

可能吗?

您可以添加特定的路线,例如(假设您还希望默认路线为/Home/Index

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

我可以为所有 none 个有效地址创建一个 catchall 吗? 像 xxx.com/Profile/Social123 不应该给服务器错误,而是重定向到家庭控制器等。