是否可以根据 url 获取控制器和操作(不是他们的名字!!)?
我发现了很多关于根据 url 获取控制器名称和方法的线程,我也做到了这一点。我可以根据方法的名称自动从 MVC 引擎获取方法的 MethodInfo,还是必须执行 Type.GetType("Namespace.Controllers."+cname+"Controller").GetMethod(mname)
Type.GetType("Namespace.Controllers."+cname+"Controller")?这不太好,因为我如何知道框架类中的名称空间?我如何知道是否遵守默认命名模式,或者是否正在使用不同的配置?
我想要得到“MVC 会执行什么?”这样的结果......
是否可以?
编辑:更多信息:
我有一个框架,它使用可翻译的、数据驱动的 url,并且有一个自定义的 url 重写。现在,当我想显示新闻对象的 url 时,它可以完美地工作,我只需编写 @Url.Content("~/"[email protected])
,并且在 url 中显示“SomeNewsCategory/SomeNews”而不是“News/29”,无需更改RouteTable.动态路由。然而,当我尝试将 RedirectToAction("SomeStaticPage","Contact"); 写入控制器时,又出现了问题。为此,我需要在数据库中注册一个静态链接,将其定位为“/SomeStaticPage/Contact”,然后写入
Redirect("~/"+DB.Load(linkid).Link);
当我有 30 个这样的 ID 时,这不太好。团队中的网络程序员开始注册“假网址”,如下所示:
public class FakeURL
{
public string Controller;
public string Action;
public int LinkID;
}
然后他像 Redirect(GetFakeUrl("controller","action"));
这样使用它,就成功了,但还是不太好。现在我开始思考,如果我将 [Link(linkid)]
属性应用于每个静态链接方法,然后覆盖基本控制器中的 RedirectToAction 方法,并且当他写 ReturnToAction("action","controller"
),我实际上会查找属性,加载 url 等。但是我还没有找到一种方法来根据控制器和操作的名称或它们的 url。
编辑:我自己编写了反射,唯一缺少的是从剃刀助手内部获取应用程序的程序集,因为 CallingAssembly是 .cshtml 的动态编译程序集,而不是我的 WebApplication。我能以某种方式得到它吗?
I have found a dozens of threads about getting the name of the controller and method based on the url, I managed that just as well. Can I get the MethodInfo of the method based on their name automatically from the MVC engine, or do I have to do Type.GetType("Namespace.Controllers."+cname+"Controller").GetMethod(mname)
? Which is not so nice, since how do I know the namespace in a framework class? How do I know if the default naming patterns are being observed, or is there a different config in use?
I want to get a "What Would MVC execute?" kind of result....
Is it possible?
EDIT: further info:
I have a framework which uses translatable, data-driven urls, and has a custom url rewriting in place. Now it works perfectly when I want to show the url of a news object, I just write @Url.Content("~/"[email protected])
, and it displays "SomeNewsCategory/SomeNews" instead of "News/29" in the url, without the need to change the RouteTable.Routes dynamically. However in turn there is a problem when I try to write RedirectToAction("SomeStaticPage","Contact");
into a controller. For that, I need to register a static link in db, have it target "/SomeStaticPage/Contact", and then writeRedirect("~/"+DB.Load(linkid).Link);
and that's just not nice, when I have 30 of these IDs. The web programmer guy in the team started registering "fake urls", which looked like this:
public class FakeURL
{
public string Controller;
public string Action;
public int LinkID;
}
and then he used it like Redirect(GetFakeUrl("controller","action"));
which did the trick, but still was not nice. Now it got me thinking, if I apply a [Link(linkid)]
attribute to each statically linked method, then override the RedirectToAction method in the base controller, and when he writes ReturnToAction("action","controller"
), I'll actually look up the attribute, load the url, etc. But I'm yet to find a way to get the methodInfo based on the names of the controller and the action, or their url.
EDIT: I've written the reflection by myself, the only thing missing is getting my application's assembly from inside a razor helper, because the CallingAssembly is the dinamically compiled assembly of the .cshtml, not my WebApplication. Can I somehow get that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要回答您的编辑,您可以编写
typeof(SomeType).Assembly
,其中SomeType
是项目中代码中定义的任何类型(例如,MvcApplication
,或任何模型或控制器)此外,您可以编写编辑 这不是您想要做的。ControllerContext.Controller.GetType()
(或ViewContext
)来获取当前请求的控制器类型To answer your edit, you can write
typeof(SomeType).Assembly
, whereSomeType
is any type defined in code in the project (eg,MvcApplication
, or any model or controller)Also, you can writeEDIT That's not what you're trying to do.ControllerContext.Controller.GetType()
(orViewContext
) to get the controller type of the current request我发现这是完全错误的做法。我试图根据名称找到控制器的类型,但实际上我一直都有该类型。
因此,我将使用
@Url.MyAction((SomeController c)=>c.SomeAction()) 代替
@Url.Action("SomeAction","SomeController")
code>,所以我什至不必找到控制器。I found out that it was totally wrong approach. I tried to find the type of the controller based on the name, when instead I had the type all along.
So instead of
@Url.Action("SomeAction","SomeController")
I'll use@Url.MyAction((SomeController c)=>c.SomeAction())
, so I won't even have to find the controller.