是否有可能得到的控制器和行动(而不是他们的名字!)的基础上的网址是什么? [英] Is it possible to get the controller and the action (NOT THEIR NAME!!) based on the url?

查看:70
本文介绍了是否有可能得到的控制器和行动(而不是他们的名字!)的基础上的网址是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经找到了几十个线程有关获取基于URL中的控制器和控制方法的名字,我认为管理的一样好。我可以从MVC引擎获取方法的MethodInfo的基础上他们的名字自动,或做我必须做的 Type.GetType(Namespace.Controllers。+ CNAME +控制器)。实现getMethod(MNAME )?这是不是很好,因为我怎么知道在一个框架类的命名空间?我怎么知道如果被观察的默认命名模式,还是有使用一个不同的配置?结果
 我希望得到一个的什么MVC会执行?的样的结果....结果
这可能吗?

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?

编辑:进一步信息:结果
我有一个使用翻译的,数据驱动的URL,并具有自定义URL到位重写一个框架。现在,它完美的作品时,我想表明的新闻对象的url,我只是写 @ Url.Content(〜/+ @ Model.Link),它显示SomeNewsCategory / SomeNews,而不是在URL新闻/ 29,而不需要动态改变RouteTable.Routes。但是又存在一个问题,当我尝试写 RedirectToAction(SomeStaticPage,联系方式); 到控制器。对于这一点,我需要注册数据库中的静态链接,有它的目标/ SomeStaticPage /联系,然后写
重定向(〜/+ DB.Load(的linkID).LINK); 这只是不是很好,当我有这些ID的30。在球队的Web程序员家伙开始报到假的URL,它是这样的:

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("~/"+@Model.Link), 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 write Redirect("~/"+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;
}

,然后他用它像重定向(GetFakeUrl(控制器,行动)); 其中的伎俩,但仍然不是很好。现在,让我思考,如果我申请一个 [链接(的linkID)] 属性静态连接的方法,然后点击覆盖在基本控制器的RedirectToAction方法,当他写 ReturnToAction(行动,控制器),我居然抬头看属性,加载URL等,但我还没有找到一种方法基于控制器和动作,或者他们的URL的名称来获得的MethodInfo。

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.

修改:我写我自己的倒影, 缺少从剃刀帮手里面让我的应用程序的组件的唯一的事情因为CallingAssembly是.cshtml的dinamically编译的程序集,而不是我的WebApplication。我能以某种方式得到?

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?

推荐答案

我发现这是完全错误的做法。我试图找到基于名称的控制器的类型,而不是当我把所有沿类型。

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.

所以不是的@ Url.Action(SomeAction,SomeController)我会使用 @ Url.MyAction((SomeControllerÇ )=> c.SomeAction()),所以我甚至不会有找到控制器。

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.

这篇关于是否有可能得到的控制器和行动(而不是他们的名字!)的基础上的网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆