如何使用路由仅显示 url 中的名称而不是 mvc.net 中的 ID

How to display only name in url instead of id in mvc.net using routing

我 url 喜欢 http://localhost:49671/TestRoutes/Display?f=hi&i=2

我想要http://localhost:49671/TestRoutes/Display/hi

我从 Index 方法调用它。

[HttpPost]
public ActionResult Index(int? e )
{
    //  return View("Display", new { f = "hi", i = 2 });
    return RedirectToAction("Display", new { f = "hi", i = 2 });
}

索引视图

@model Try.Models.TestRoutes
@using (Html.BeginForm())
{
    Model.e = 5 ; 
    <input type="submit" value="Create" class="btn btn-default" />
}

显示操作方法

// [Route("TestRoutes/{s}")]
public ActionResult Display(string s, int i)
{
    return View(); 
}

路由配置文件

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Professional", // Route name
        "{controller}/{action}/{id}/{name}", // URL with parameters
         new { controller = "TestRoutes", action = "Display", s = UrlParameter.Optional, i = UrlParameter.Optional
    });
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id =   UrlParameter.Optional
    });

将您的路线更改为 routes.MapRoute( "Professional", // 路线名称 "{controller}/{action}/{name}", // URL 带参数 新的 { 控制器 = "TestRoutes", 动作 = "Display" } // 参数默认值 );

你的行为是

public ActionResult Display(string name)
{
     //action goes here
}

删除 maproute 代码: routes.MapRoute( "Professional", // 路线名称 "{controller}/{action}/{id}/{name}", // URL 带参数 新 { 控制器 = "TestRoutes", 动作 = "Display", s = UrlParameter.Optional, i = UrlParameter.Optional }); 使用属性路由代码: [路线("TestRoutes/{s}/{i?}")] public ActionResult Display(string s, int?i) { return查看(); }

您需要将路线定义更改为

routes.MapRoute(
    name: "Professional",
    url: "TestRoutes/Display/{s}/{i}",
    default: new { controller = "TestRoutes", action = "Display", i = UrlParameter.Optional }
);

以便占位符的名称与您的方法中的参数名称相匹配。另请注意,只有最后一个参数可以标记为 UrlParameter.Optional(否则 RoutingEngine 无法匹配段并且值将添加为查询字符串参数,而不是路由值)

然后您需要更改控制器方法以匹配 route/method 参数

[HttpPost]
public ActionResult Index(int? e )
{
    return RedirectToAction("Display", new { s = "hi", i = 2 }); // s not f
}

您也可以尝试使用属性路由。您可以使用属性路由更轻松地控制路由。

首先像这样改变你的RouteConfig.cs:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        //);
    }
}

之后像这样更改您的控制器文件:

namespace YourProjectName.Controllers
{
    [RoutePrefix("Home")]
    [Route("{action}/{id=0}")]
    public class HomeController : Controller
    {
        [Route("Index")]
        public ActionResult Index()
        {
            return View();
        }

        [Route("ChangeAddress/{addressID}")]
        public ActionResult ChangeAddress(int addressID)
        {
            //your codes to change address
        }
}

您还可以在此 post 中了解有关属性路由的更多信息: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/

解决这个问题的另一种方法是将正确的路由放在默认路由之前,如下所示:

routes.MapRoute(name: "MyRouteName", url: "Id", defaults: new { controller= "Home", action = "Index",id= Id });

默认路线:

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