ASP.NET-MVC 。如何从 url 获取控制器名称?

发布于 2024-08-07 19:30:28 字数 460 浏览 6 评论 0原文

如何使用我在 Global.asax 中定义的路由获取相对 Url 的控制器名称?

示例:

如果我有一个像这样的路由定义:

routes.MapRoute(
                "Default",                                              // Route name
                "{language}/{controller}/{action}/{id}",                 // URL with parameters
                new { controller = "Home", action = "Index", id = "", language = "en" }

从字符串“~/en/products/list”我想要有产品(控制器名称)。是否有现有的方法可以做到这一点?

How can I get the controller name of a relative Url, using the routes I have defined in Global.asax?

Example:

if I have a route defiend like this:

routes.MapRoute(
                "Default",                                              // Route name
                "{language}/{controller}/{action}/{id}",                 // URL with parameters
                new { controller = "Home", action = "Index", id = "", language = "en" }

from the string "~/en/products/list" I want to have products (the controller name). Is there any existing method that already does this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-08-14 19:30:28

您可能应该像乔治建议的那样添加另一条路线,但如果您确实只需要从路线派生的控制器值,您可以在控制器操作方法中执行此操作:

var controller = (string)RouteData.Values["controller"];

You should probably add another route like George suggests but if you really just need the controller value derived from the route you can do this in your controller action methods:

var controller = (string)RouteData.Values["controller"];
冰火雁神 2024-08-14 19:30:28

请参阅 Stephen Walther 的博客文章 ASP.NET MVC 技巧 #13 – 对自定义路由进行单元测试

项目 MvcFakes 有一个旧的 System.Web.Abstractions 引用。所以你必须更换它
使用新的并重新编译项目以获取 MvcFakes.dll。

这是我的代码:

public string getControllerNameFromUrl()
{
    RouteCollection rc = new RouteCollection();
    MvcApplication.RegisterRoutes(rc);
    System.Web.Routing.RouteData rd = new RouteData();
    var context = new FakeHttpContext("\\" + HttpContext.Request.Url.AbsolutePath);
    rd = rc.GetRouteData(context);
    return rd.Values["action"].ToString();
}

在我上面的代码中,“MvcApplication”是Global.asax中的类名。

祝你好运 !

See Stephen Walther's blog post ASP.NET MVC Tip #13 – Unit Test Your Custom Routes

The project MvcFakes has an old System.Web.Abstractions reference. So you must replace it
with the new one and recomply the project to get MvcFakes.dll.

This is my code:

public string getControllerNameFromUrl()
{
    RouteCollection rc = new RouteCollection();
    MvcApplication.RegisterRoutes(rc);
    System.Web.Routing.RouteData rd = new RouteData();
    var context = new FakeHttpContext("\\" + HttpContext.Request.Url.AbsolutePath);
    rd = rc.GetRouteData(context);
    return rd.Values["action"].ToString();
}

In my code above "MvcApplication" is the class name in the Global.asax.

Good luck !

浅忆流年 2024-08-14 19:30:28

我不确定你在问什么,所以如果我的答案是错误的,那是因为我在猜测你想要什么。

您随时可以向 Global.asax 添加另一条路由。这通常是处理“异常”案件的最简单方法。

如果您想返回产品列表,您将使用此路由:

routes.MapRoute(
            "ProductList",         
            "{language}/{products}/{action}/",
            new { controller = "Products", action = "List", language = "en" });

如果不止一种类型的实体要使用此路由,您还可以将产品替换为更通用的 {controller}。您应该根据您的需要修改它。

例如,要使其成为可用于获取任何产品列表的通用路由:

routes.MapRoute(
            "ProductList",         
            "{language}/{controller}/{action}/",
            new { controller = "Products", action = "List", language = "en" });

它的作用是创建一条路由(您应始终将其放置在 Default 路由之前),其中包含,“无论用户输入什么,都给我他们要求的控制器和操作”。 (例如 /en/Products/List/en/Users/List)。

要访问该控制器,您只需导航到以下位置:yoursite.com/en/products/list。您还可以使用 HTMLActionLink访问控制器。

<%=Html.ActionLink("产品", "列表", new {controller = 产品}, null ) %>

我在没有打开 IDE 的情况下编写此内容,因此 ActionLink 可能有错误。

I'm not sure what you're asking, so if my answer's wrong, it's because I'm guessing at what you want.

You can always add another route to your Global.asax. That's often the easiest way to deal with cases 'outside of the norm'.

If you want to return a list of products, you'll use this route:

routes.MapRoute(
            "ProductList",         
            "{language}/{products}/{action}/",
            new { controller = "Products", action = "List", language = "en" });

You can also replace products with the more generic {controller} if more than one type of entity is going to use this route. You should modify it for your needs.

For example, to make this a generic route that you can use to get a list of any product:

routes.MapRoute(
            "ProductList",         
            "{language}/{controller}/{action}/",
            new { controller = "Products", action = "List", language = "en" });

What this does is that it creates a route (that you should always place before your Default route) that says, "For whatever the user enters, give me the controller and action they ask for". (Such as /en/Products/List, or /en/Users/List).

To visit that controller, you simply need to navigate to the following: yoursite.com/en/products/list. You can also use the HTMLActionLink to visit the controller.

<%=Html.ActionLink("Product", "List", new { controller = Products }, null ) %>

I'm writing this without my IDE open, so the ActionLink may have an error in it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文