ASP.NET-MVC 。如何从 url 获取控制器名称?
如何使用我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能应该像乔治建议的那样添加另一条路线,但如果您确实只需要从路线派生的控制器值,您可以在控制器操作方法中执行此操作:
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:
请参阅 Stephen Walther 的博客文章 ASP.NET MVC 技巧 #13 – 对自定义路由进行单元测试
项目 MvcFakes 有一个旧的 System.Web.Abstractions 引用。所以你必须更换它
使用新的并重新编译项目以获取 MvcFakes.dll。
这是我的代码:
在我上面的代码中,“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:
In my code above "MvcApplication" is the class name in the Global.asax.
Good luck !
我不确定你在问什么,所以如果我的答案是错误的,那是因为我在猜测你想要什么。
您随时可以向
Global.asax
添加另一条路由。这通常是处理“异常”案件的最简单方法。如果您想返回产品列表,您将使用此路由:
如果不止一种类型的实体要使用此路由,您还可以将产品替换为更通用的
{controller}
。您应该根据您的需要修改它。例如,要使其成为可用于获取任何产品列表的通用路由:
它的作用是创建一条路由(您应始终将其放置在
Default
路由之前),其中包含,“无论用户输入什么,都给我他们要求的控制器和操作”。 (例如/en/Products/List
或/en/Users/List
)。要访问该控制器,您只需导航到以下位置:
yoursite.com/en/products/list
。您还可以使用HTMLActionLink
访问控制器。我在没有打开 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:
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:
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 theHTMLActionLink
to visit the controller.I'm writing this without my IDE open, so the
ActionLink
may have an error in it.