【问题标题】:How to Change ASP.NET MVC Controller Name in URL?如何更改 URL 中的 ASP.NET MVC 控制器名称?
【发布时间】:2016-01-25 11:25:03
【问题描述】:

如果我们有 "example_name" 我们可以使用 [ActionName("")] 在 url 中更改它所以,我想为控制器名称执行此操作。

我可以这样做:

ControllerName > example_nameController > 在 URL:"/example_controller"

我想在 URL 中更改这样的控制器名称:"/example-conroller"

【问题讨论】:

  • 嗨,重定向请看我的更新答案

标签: c# asp.net-mvc url controller


【解决方案1】:

user449689的回答很好,但他忘了说你需要添加

routes.MapMvcAttributeRoutes();

进入 RouteConfig.cs 的 RegisterRoutes()

【讨论】:

  • 不错的补充,还值得一提的是,此行必须放在任何 routes.MapRoute 调用之前才能正常工作。
【解决方案2】:

你可以在Routes.cs中指定

 routes.MapRoute(
 name: "College",
 url: "Student/{studentId}",
 defaults: new { controller = "Student", action = "Details"}
 );

我们可以定义这样一个约束为

  routes.MapRoute(
  name: "College",
  url: "Student/{studentId}", 
  defaults: new { controller = "Student", action = "Details"},
  constraints:new{id=@"\d+"} 
  ); 

【讨论】:

  • routes.MapRoute("ClientDetailLevel1", "Clients/{id}", new { controller = "Clients", action = "Index" });
【解决方案3】:

您需要使用 Attribute Routing,这是 MVC 5 中引入的功能。

根据您的示例,您应该如下编辑控制器:

[RoutePrefix("example-name")]
public class example_nameController : Controller
{
    // Route: example-name/Index
    [Route]
    public ActionResult Index()
    {
        return View();
    }

    // Route: example-name/Contact
    [Route]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

使用控制器顶部的RoutePrefix 属性将允许您在整个控制器上定义路由。

如前所述,此功能在 MVC 5 中本机可用,如果您使用的是先前版本的 MVC,则需要添加以下 NuGet 包:AttributeRouting 并在您的控制器中添加以下 using:

using AttributeRouting;
using AttributeRouting.Web.Mvc;


如果您有另一个名为 example_name2Controller 的控制器,并且您想添加一个链接到它的超链接,您可以轻松地执行以下操作:
@Html.ActionLink("Go to example-name2", "Index", "example_name2");

您不需要调用将重定向到example_name2Controller 的操作,但如果您在其他场合需要这样做,您可以这样做:

public ActionResult RedirectToExample_Name2Controller()
{
    return RedirectToAction("Index", "example_name2");
}

【讨论】:

  • 感谢您的详细回答。它更好,它帮助了我。我有另一个问题。我有不同的控制器,比如说“example_controller2”,当我点击“Contact”时,我想去“example_controller2”,我搜索了,我想我会使用“RedirectToRoute”。这有点像“RedirectToAction”,但我没有成功。
  • 我知道这是旧的,但我需要包含 using RoutePrefixAttribute = AttributeRouting.RoutePrefixAttribute; 以澄清系统路由前缀属性的歧义(对于 MVC
【解决方案4】:

您可以使用Attribute Routing

[RoutePrefix("Users")]
public class HomeController : Controller
{
    //Route: Users/Index
    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

【讨论】:

  • 谢谢。我正在使用 mvc 5,这是更好的解决方案。
【解决方案5】:

您可以通过Routes.cs 进行此操作

routes.MapRoute(
      name: "Controller",
      url: "example-controller/{action}",
      defaults: new { 
      controller = "ControllerName", action ="Index"
      }   
);

还有一种方法,如果你看这个问题的答案:How to achieve a dynamic controller and action method in ASP.NET MVC?

【讨论】:

  • 谢谢。对于这个例子;我们将创建很多地图路线。我正在寻找不同的解决方案?
  • @Cagatay 更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多